home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 July / CMCD0704.ISO / Software / Freeware / Comunicatii / htttrack / httrack-3.32-2.exe / {app} / src / htscoremain.c < prev    next >
Encoding:
C/C++ Source or Header  |  2004-05-08  |  92.8 KB  |  2,478 lines

  1. /* ------------------------------------------------------------ */
  2. /*
  3. HTTrack Website Copier, Offline Browser for Windows and Unix
  4. Copyright (C) Xavier Roche and other contributors
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20.  
  21. Important notes:
  22.  
  23. - We hereby ask people using this source NOT to use it in purpose of grabbing
  24. emails addresses, or collecting any other private information on persons.
  25. This would disgrace our work, and spoil the many hours we spent on it.
  26.  
  27.  
  28. Please visit our Website: http://www.httrack.com
  29. */
  30.  
  31.  
  32. /* ------------------------------------------------------------ */
  33. /* File: httrack.c subroutines:                                 */
  34. /*       main routine (first called)                            */
  35. /* Author: Xavier Roche                                         */
  36. /* ------------------------------------------------------------ */
  37.  
  38. /* Internal engine bytecode */
  39. #define HTS_INTERNAL_BYTECODE
  40.  
  41. #include "htscoremain.h"
  42.  
  43. #include "htsglobal.h"
  44. #include "htscore.h"
  45. #include "htsdefines.h"
  46. #include "htsalias.h"
  47. #include "htswrap.h"
  48. #include "htsmodules.h"
  49. #include "htszlib.h"
  50.  
  51. #include <ctype.h>
  52. #if HTS_WIN
  53. #else
  54. #ifndef HTS_DO_NOT_USE_UID
  55. /* setuid */
  56. #include <pwd.h>
  57. #ifdef HAVE_UNISTD_H
  58. #include <unistd.h>
  59. #endif
  60. #endif
  61. #endif
  62.  
  63. extern int exit_xh;          // sortir prΘmaturΘment
  64.  
  65. /* Resolver */
  66. extern int IPV6_resolver;
  67.  
  68.  
  69. // Add a command in the argc/argv
  70. #define cmdl_add(token,argc,argv,buff,ptr) \
  71.   argv[argc]=(buff+ptr); \
  72.   strcpybuff(argv[argc],token); \
  73.   ptr += (strlen(argv[argc])+2); \
  74.   argc++
  75.  
  76. // Insert a command in the argc/argv
  77. #define cmdl_ins(token,argc,argv,buff,ptr) \
  78.   { \
  79.   int i; \
  80.   for(i=argc;i>0;i--)\
  81.   argv[i]=argv[i-1];\
  82.   } \
  83.   argv[0]=(buff+ptr); \
  84.   strcpybuff(argv[0],token); \
  85.   ptr += (strlen(argv[0])+2); \
  86.   argc++
  87.  
  88. #define htsmain_free() do { if (url != NULL) { free(url); } } while(0)
  89.  
  90. #define ensureUrlCapacity(url, urlsize, size) do { \
  91.   if (urlsize < size || url == NULL) { \
  92.     urlsize = size; \
  93.     if (url == NULL) { \
  94.       url = (char*) malloct(urlsize); \
  95.       if (url != NULL) url[0]='\0'; \
  96.     } else { \
  97.       url = (char*) realloct(url, urlsize); \
  98.     } \
  99.     if (url == NULL) { \
  100.       HTS_PANIC_PRINTF("* memory exhausted"); \
  101.       htsmain_free(); \
  102.       return -1; \
  103.     } \
  104.   } \
  105. } while(0)
  106.  
  107. static void set_wrappers(void) {
  108. #if HTS_ANALYSTE
  109.   // custom wrappers
  110.   hts_htmlcheck_init         = (t_hts_htmlcheck_init)           htswrap_read("init");
  111.   hts_htmlcheck_uninit       = (t_hts_htmlcheck_uninit)         htswrap_read("free");
  112.   hts_htmlcheck_start        = (t_hts_htmlcheck_start)          htswrap_read("start");
  113.   hts_htmlcheck_end          = (t_hts_htmlcheck_end)            htswrap_read("end");
  114.   hts_htmlcheck_chopt        = (t_hts_htmlcheck_chopt)          htswrap_read("change-options");
  115.   hts_htmlcheck              = (t_hts_htmlcheck)                htswrap_read("check-html");
  116.   hts_htmlcheck_query        = (t_hts_htmlcheck_query)          htswrap_read("query");
  117.   hts_htmlcheck_query2       = (t_hts_htmlcheck_query2)         htswrap_read("query2");
  118.   hts_htmlcheck_query3       = (t_hts_htmlcheck_query3)         htswrap_read("query3");
  119.   hts_htmlcheck_loop         = (t_hts_htmlcheck_loop)           htswrap_read("loop");
  120.   hts_htmlcheck_check        = (t_hts_htmlcheck_check)          htswrap_read("check-link");
  121.   hts_htmlcheck_pause        = (t_hts_htmlcheck_pause)          htswrap_read("pause");
  122.   hts_htmlcheck_filesave     = (t_hts_htmlcheck_filesave)       htswrap_read("save-file");
  123.   hts_htmlcheck_linkdetected = (t_hts_htmlcheck_linkdetected)   htswrap_read("link-detected");
  124.   hts_htmlcheck_xfrstatus    = (t_hts_htmlcheck_xfrstatus)      htswrap_read("transfer-status");
  125.   hts_htmlcheck_savename     = (t_hts_htmlcheck_savename)       htswrap_read("save-name");
  126.   hts_htmlcheck_sendhead     = (t_hts_htmlcheck_sendhead)       htswrap_read("send-header");
  127.   hts_htmlcheck_receivehead  = (t_hts_htmlcheck_receivehead)    htswrap_read("receive-header");
  128. #endif
  129. }
  130.  
  131. // Main, rΘcupΦre les paramΦtres et appelle le robot
  132. #if HTS_ANALYSTE
  133. HTSEXT_API int hts_main(int argc, char **argv) {
  134. #else
  135. int main(int argc, char **argv) {
  136. #endif
  137.   char** x_argv=NULL;     // Patch pour argv et argc: en cas de rΘcupΘration de ligne de commande
  138.   char* x_argvblk=NULL;   // (reprise ou update)
  139.   int   x_ptr=0;          // offset
  140.   //
  141.   int argv_url=-1;           // ==0 : utiliser cache et doit.log
  142.   char* argv_firsturl=NULL;  // utilisΘ pour nommage par dΘfaut
  143.   char* url = NULL;          // URLS sΘparΘes par un espace
  144.   int   url_sz = 65535;
  145.   //char url[65536];         // URLS sΘparΘes par un espace
  146.   // the parametres
  147.   httrackp httrack;
  148.   int httrack_logmode=3;   // ONE log file
  149.   int recuperer=0;       // rΘcupΘrer un plantage (n'arrive jamais, α supprimer)
  150. #if HTS_WIN
  151. #if HTS_ANALYSTE!=2
  152.   WORD   wVersionRequested; /* requested version WinSock API */ 
  153.   WSADATA wsadata;        /* Windows Sockets API data */
  154. #endif
  155. #else
  156. #ifndef HTS_DO_NOT_USE_UID
  157.   int switch_uid=-1,switch_gid=-1;      /* setuid/setgid */
  158. #endif
  159.   int switch_chroot=0;                  /* chroot ? */
  160. #endif
  161.   //
  162.   ensureUrlCapacity(url, url_sz, 65536);
  163.   //
  164.  
  165. #if HTS_ANALYSTE
  166.   // custom wrappers
  167.   set_wrappers();
  168. #endif
  169.  
  170.   // options par dΘfaut
  171.   memset(&httrack, 0, sizeof(httrackp));
  172.   httrack.wizard=2;   // wizard automatique
  173.   httrack.quiet=0;     // questions
  174.   //  
  175.   httrack.travel=0;   // mΩme adresse
  176.   httrack.depth=9999; // mirror total par dΘfaut
  177.   httrack.extdepth=0; // mais pas α l'extΘrieur
  178.   httrack.seeker=1;   // down 
  179.   httrack.urlmode=2;  // relatif par dΘfaut
  180.   httrack.debug=0;    // pas de dΘbug en plus
  181.   httrack.getmode=3;  // linear scan
  182.   httrack.maxsite=-1; // taille max site (aucune)
  183.   httrack.maxfile_nonhtml=-1; // taille max fichier non html
  184.   httrack.maxfile_html=-1;    // idem pour html
  185.   httrack.maxsoc=4;     // nbre socket max
  186.   httrack.fragment=-1;  // pas de fragmentation
  187.   httrack.nearlink=0;   // ne pas prendre les liens non-html "adjacents"
  188.   httrack.makeindex=1;  // faire un index
  189.   httrack.kindex=0;     // index 'keyword'
  190.   httrack.delete_old=1; // effacer anciens fichiers
  191.   httrack.makestat=0;  // pas de fichier de stats
  192.   httrack.maketrack=0; // ni de tracking
  193.   httrack.timeout=120; // timeout par dΘfaut (2 minutes)
  194.   httrack.cache=1;     // cache prioritaire
  195.   httrack.shell=0;     // pas de shell par defaut
  196.   httrack.proxy.active=0;    // pas de proxy
  197.   strcpybuff(httrack.proxy.bindhost, "");  // bind default host
  198.   httrack.user_agent_send=1; // envoyer un user-agent
  199.   strcpybuff(httrack.user_agent,"Mozilla/4.5 (compatible; HTTrack 3.0x; Windows 98)");
  200.   strcpybuff(httrack.referer, "");
  201.   strcpybuff(httrack.from, "");
  202.   httrack.savename_83=0;     // noms longs par dΘfaut
  203.   httrack.savename_type=0;   // avec structure originale
  204.   httrack.mimehtml=0;        // pas MIME-html
  205.   httrack.parsejava=1;       // parser classes
  206.   httrack.hostcontrol=0;     // PAS de control host pour timeout et traffic jammer
  207.   httrack.retry=2;           // 2 retry par dΘfaut
  208.   httrack.errpage=1;         // copier ou gΘnΘrer une page d'erreur en cas d'erreur (404 etc.)
  209.   httrack.check_type=1;      // vΘrifier type si inconnu (cgi,asp..) SAUF / considΘrΘ comme html
  210.   httrack.all_in_cache=0;    // ne pas tout stocker en cache
  211.   httrack.robots=2;          // traiter les robots.txt
  212.   httrack.external=0;        // liens externes normaux
  213.   httrack.passprivacy=0;     // mots de passe dans les fichiers
  214.   httrack.includequery=1;    // include query-string par dΘfaut
  215.   httrack.mirror_first_page=0;  // pas mode mirror links
  216.   httrack.accept_cookie=1;   // gΘrer les cookies
  217.   httrack.cookie=NULL;
  218.   httrack.http10=0;          // laisser http/1.1
  219.   httrack.nokeepalive = 0;   // pas keep-alive
  220.   httrack.nocompression=0;   // pas de compression
  221.   httrack.tolerant=0;        // ne pas accepter content-length incorrect
  222.   httrack.parseall=1;        // tout parser (tags inconnus, par exemple)
  223.   httrack.parsedebug=0;      // pas de mode dΘbuggage
  224.   httrack.norecatch=0;       // ne pas reprendre les fichiers effacΘs par l'utilisateur
  225.   httrack.verbosedisplay=0;  // pas d'animation texte
  226.   httrack.sizehack=0;        // size hack
  227.   httrack.urlhack=1;         // url hack (normalizer)
  228.   strcpybuff(httrack.footer,HTS_DEFAULT_FOOTER);
  229.   httrack.ftp_proxy=1;       // proxy http pour ftp
  230.   strcpybuff(httrack.filelist,"");
  231.   strcpybuff(httrack.lang_iso,"en, *");
  232.   strcpybuff(httrack.mimedefs,"\n"); // aucun filtre mime (\n IMPORTANT)
  233.   //
  234.   httrack.log=stdout;
  235.   httrack.errlog=stderr;
  236.   httrack.flush=1;           // flush sur les fichiers log
  237.   //httrack.aff_progress=0;
  238.   httrack.keyboard=0;
  239.   //
  240.   strcpybuff(httrack.path_html,"");
  241.   strcpybuff(httrack.path_log,"");
  242.   strcpybuff(httrack.path_bin,"");
  243.   //
  244. #if HTS_SPARE_MEMORY==0
  245.   httrack.maxlink=100000;    // 100,000 liens max par dΘfaut (400Kb)
  246.   httrack.maxfilter=200;     // 200 filtres max par dΘfaut
  247. #else
  248.   httrack.maxlink=10000;     // 10,000 liens max par dΘfaut (40Kb)
  249.   httrack.maxfilter=50;      // 50 filtres max par dΘfaut
  250. #endif
  251.   httrack.maxcache=1048576*32;  // a peu prΦs 32Mo en cache max -- OPTION NON PARAMETRABLE POUR L'INSTANT --
  252.   //httrack.maxcache_anticipate=256;  // maximum de liens α anticiper
  253.   httrack.maxtime=-1;        // temps max en secondes
  254.   httrack.maxrate=-1;        // pas de taux maxi
  255.   httrack.maxconn=10;        // nombre connexions/s
  256.   httrack.waittime=-1;       // wait until.. hh*3600+mm*60+ss
  257.   //
  258.   httrack.exec=argv[0];
  259.   httrack.is_update=0;      // not an update (yet)
  260.   httrack.dir_topindex=0;   // do not built top index (yet)
  261.   //
  262.   httrack.state.stop=0;     // stopper
  263.   httrack.state.exit_xh=0;  // abort
  264.   //
  265.   _DEBUG_HEAD=0;            // pas de debuggage en tΩtes
  266.  
  267.   
  268. #if HTS_WIN
  269. #if HTS_ANALYSTE!=2
  270.   {
  271.     int stat;
  272.     wVersionRequested = 0x0101;
  273.     stat = WSAStartup( wVersionRequested, &wsadata );
  274.     if (stat != 0) {
  275.       HTS_PANIC_PRINTF("Winsock not found!\n");
  276.       htsmain_free();
  277.       return -1;
  278.     } else if (LOBYTE(wsadata.wVersion) != 1  && HIBYTE(wsadata.wVersion) != 1) {
  279.       HTS_PANIC_PRINTF("WINSOCK.DLL does not support version 1.1\n");
  280.       WSACleanup();
  281.       htsmain_free();
  282.       return -1;
  283.     }
  284.   }
  285. #endif
  286. #endif
  287.  
  288.   /* Init root dir */
  289.   hts_rootdir(argv[0]);
  290.  
  291. #if HTS_WIN
  292. #else
  293.   /* Terminal is a tty, may ask questions and display funny information */
  294.   if (isatty(1)) {
  295.     httrack.quiet=0;
  296.     httrack.verbosedisplay=1;
  297.   }
  298.   /* Not a tty, no stdin input or funny output! */
  299.   else {
  300.     httrack.quiet=1;
  301.     httrack.verbosedisplay=0;
  302.   }
  303. #endif
  304.  
  305.   /* First test: if -#R then only launch ftp */
  306.   if (argc > 2) {
  307.     if (strcmp(argv[1],"-#R")==0) {
  308.       if (argc==6) {
  309.         lien_back r;
  310.         char* path;
  311.         FILE* fp;
  312.         strcpybuff(r.url_adr,argv[2]);
  313.         strcpybuff(r.url_fil,argv[3]);
  314.         strcpybuff(r.url_sav,argv[4]);
  315.         path=argv[5];
  316.         r.status=1000;
  317.         run_launch_ftp(&r);
  318.         fp=fopen(fconv(path),"wb");
  319.         if (fp) {
  320.           fprintf(fp,"%d %s",r.r.statuscode,r.r.msg);
  321.           fclose(fp); fp=NULL;
  322.           rename(fconv(path),fconcat(path,".ok"));
  323.         } else remove(fconv(path));
  324.       } else {
  325.         printf("htsftp error, wrong parameter number (%d)\n",argc);
  326.       }
  327.       exit(0);   // pas _exit()
  328.     }
  329.   }
  330.  
  331.   // ok, non ftp, continuer
  332.  
  333.  
  334.   // Binary program path?
  335. #ifndef HTS_HTTRACKDIR
  336.   {
  337.     char* path=fslash(argv[0]);
  338.     char* a;
  339.     if ((a=strrchr(path,'/'))) {
  340.       httrack.path_bin[0]='\0';
  341.       strncatbuff(httrack.path_bin,argv[0],(int) a - (int) path);
  342.     }
  343.   }
  344. #else
  345.   strcpybuff(httrack.path_bin, HTS_HTTRACKDIR);
  346. #endif
  347.  
  348.  
  349.   /* filter CR, LF, TAB.. */
  350.   {
  351.     int na;
  352.     for(na=1;na<argc;na++) {
  353.       char* a;
  354.       while( (a=strchr(argv[na],'\x0d')) ) *a=' ';
  355.       while( (a=strchr(argv[na],'\x0a')) ) *a=' ';
  356.       while( (a=strchr(argv[na],9)) )      *a=' ';
  357.       /* equivalent to "empty parameter" */
  358.       if ((strcmp(argv[na],HTS_NOPARAM)==0) || (strcmp(argv[na],HTS_NOPARAM2)==0))        // (none)
  359.         strcpybuff(argv[na],"\"\"");
  360.       if (strncmp(argv[na],"-&",2)==0)
  361.         argv[na][1]='%';
  362.     }
  363.   }
  364.  
  365.  
  366.  
  367.   /* create x_argvblk buffer for transformed command line */
  368.   {
  369.     int current_size=0;
  370.     int size;
  371.     int na;
  372.     for(na=0;na<argc;na++)
  373.       current_size += (strlen(argv[na]) + 1);
  374.     if ((size=fsize("config"))>0)
  375.       current_size += size;
  376.     x_argvblk=(char*) malloct(current_size+32768);
  377.     if (x_argvblk == NULL) {
  378.       HTS_PANIC_PRINTF("Error, not enough memory");
  379.       htsmain_free();
  380.       return -1;
  381.     }
  382.     x_argvblk[0]='\0';
  383.     x_ptr=0;
  384.  
  385.     /* Create argv */
  386.     x_argv = (char**) malloct(sizeof(char*) * ( argc + 1024 ));
  387.   }
  388.  
  389.   /* Create new argc/argv, replace alias, count URLs, treat -h, -q, -i */
  390.   {
  391.     char _tmp_argv[2][HTS_CDLMAXSIZE];
  392.     char* tmp_argv[2];
  393.     char tmp_error[HTS_CDLMAXSIZE];
  394.     int tmp_argc;
  395.     int x_argc=0;
  396.     int na;
  397.     tmp_argv[0]=_tmp_argv[0];
  398.     tmp_argv[1]=_tmp_argv[1];
  399.     //
  400.     argv_url=0;       /* pour comptage */
  401.     //
  402.     cmdl_add(argv[0],x_argc,x_argv,x_argvblk,x_ptr);
  403.     na=1;             /* commencer aprΦs nom_prg */
  404.     while(na<argc) {
  405.       int result=1;
  406.       tmp_argv[0][0]=tmp_argv[1][0]='\0';
  407.  
  408.       /* VΘrifier argv[] non vide */
  409.       if (strnotempty(argv[na])) {
  410.         
  411.         /* VΘrifier Commande (alias) */
  412.         result=optalias_check(argc,(const char * const *)argv,na,
  413.           &tmp_argc,(char**)tmp_argv,tmp_error);
  414.         if (!result) {
  415.           HTS_PANIC_PRINTF(tmp_error);
  416.           htsmain_free();
  417.           return -1;
  418.         }
  419.         
  420.         /* Copier */
  421.         cmdl_add(tmp_argv[0],x_argc,x_argv,x_argvblk,x_ptr);
  422.         if (tmp_argc > 1) {
  423.           cmdl_add(tmp_argv[1],x_argc,x_argv,x_argvblk,x_ptr);
  424.         }
  425.         
  426.         /* Compter URLs et dΘtecter -i,-q.. */
  427.         if (tmp_argc == 1) {           /* pas -P & co */
  428.           if (!cmdl_opt(tmp_argv[0])) {   /* pas -c0 & co */
  429.             if (argv_url<0) argv_url=0;   // -1==force -> 1=one url already detected, wipe all previous options
  430.             //if (argv_url>=0) {
  431.             argv_url++;
  432.             if (!argv_firsturl)
  433.               argv_firsturl=x_argv[x_argc-1];
  434.             //}
  435.           } else {
  436.             if (strcmp(tmp_argv[0],"-h")==0) {
  437.               help(argv[0],!httrack.quiet);
  438.               htsmain_free();
  439.               return 0;
  440.             } else {
  441.               if (strncmp(tmp_argv[0],"--",2)) {   /* pas */
  442.                 if ((strchr(tmp_argv[0],'q')!=NULL))
  443.                   httrack.quiet=1;    // ne pas poser de questions! (nohup par exemple)
  444.                 if ((strchr(tmp_argv[0],'i')!=NULL)) {  // doit.log!
  445.                   argv_url=-1;        /* forcer */
  446.                   httrack.quiet=1;
  447.                 }
  448.               } else if (strcmp(tmp_argv[0] + 2,"quiet") == 0) {
  449.                 httrack.quiet=1;    // ne pas poser de questions! (nohup par exemple)
  450.               } else if (strcmp(tmp_argv[0] + 2,"continue") == 0) {
  451.                 argv_url=-1;        /* forcer */
  452.                 httrack.quiet=1;
  453.               }
  454.             }
  455.           }
  456.         } else if (tmp_argc == 2) {
  457.           if ((strcmp(tmp_argv[0],"-%L")==0)) {  // liste d'URLs
  458.             if (argv_url<0) argv_url=0;   // -1==force -> 1=one url already detected, wipe all previous options
  459.             //if (argv_url>=0)
  460.             argv_url++;        /* forcer */
  461.           }
  462.         }
  463.       }
  464.  
  465.       na+=result;
  466.     }
  467.     if (argv_url<0)
  468.       argv_url=0;
  469.  
  470.     /* Nouveaux argc et argv */
  471.     argv=x_argv;
  472.     argc=x_argc;
  473.   }
  474.  
  475.  
  476.  
  477.  
  478.   // Ici on ajoute les arguments de config
  479. /*
  480.   if (fexist("config")) {    // configuration        
  481.     x_argvblk2=(char*) calloct(32768,1);
  482.  
  483.     if (x_argvblk2!=NULL) {
  484.       FILE* fp;
  485.       int x_argc2;
  486.           
  487.       //strcpybuff(x_argvblk2,"httrack ");
  488.       fp=fopen("config","rb");
  489.       if (fp) {
  490.         linput(fp,x_argvblk2+strlen(x_argvblk2),32000);
  491.         fclose(fp); fp=NULL;
  492.     
  493.         // calculer arguments selon derniers arguments
  494.         x_argv2[0]=argv[0];
  495.         x_argc2=1;
  496.         {
  497.           char* p=x_argvblk2;
  498.           do {
  499.             x_argv2[x_argc2++]=p;
  500.             p=strchr(p,' ');
  501.             if (p) {
  502.               *p=0;    // octet nul (tableau)
  503.               p++;
  504.             }            
  505.           } while(p!=NULL);
  506.         }
  507.         // recopier arguments actuels (pointeurs uniquement)
  508.         {
  509.          int na;
  510.           for(na=1;na<argc;na++) {
  511.             x_argv2[x_argc2++]=argv[na];
  512.           }
  513.         }
  514.         argc=x_argc2;      // nouvel argc
  515.         argv=x_argv2;      // nouvel argv
  516.       }
  517.     }
  518.   }
  519. */
  520.  
  521.  
  522.   // Option O and includerc
  523.   { 
  524.     int loops=0;
  525.     while (loops<2) {
  526.       char* com;
  527.       int na;
  528.       
  529.       for(na=1;na<argc;na++) {
  530.         
  531.         if (argv[na][0]=='"') {
  532.           char tempo[HTS_CDLMAXSIZE];
  533.           strcpybuff(tempo,argv[na]+1);
  534.           if (tempo[strlen(tempo)-1]!='"') {
  535.             char s[HTS_CDLMAXSIZE];
  536.             sprintf(s,"Missing quote in %s",argv[na]);
  537.             HTS_PANIC_PRINTF(s);
  538.             htsmain_free();
  539.             return -1;
  540.           }
  541.           tempo[strlen(tempo)-1]='\0';
  542.           strcpybuff(argv[na],tempo);
  543.         }
  544.         
  545.         if (cmdl_opt(argv[na])) { // option
  546.           com=argv[na]+1;
  547.           
  548.           while(*com) {
  549.             switch(*com) {
  550.             case 'O':    // output path
  551.               if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  552.                 HTS_PANIC_PRINTF("Option O needs to be followed by a blank space, and a path (or path,path)");
  553.                 printf("Example: -O /binary/\n");
  554.                 printf("Example: -O /binary/,/log/\n");
  555.                 htsmain_free();
  556.                 return -1;
  557.               } else {
  558.                 char* a;
  559.                 na++;
  560.                 strcpybuff(httrack.path_html,"");
  561.                 strcpybuff(httrack.path_log,"");
  562.                 a=strstr(argv[na],"\",\"");  // rechercher en premier, au cas ou -O "c:\pipo,test","c:\test"
  563.                 if (!a)
  564.                   a=strchr(argv[na],',');  // 2 path
  565.                 else
  566.                   a++;  // position ,
  567.                 if (a) {
  568.                   strncatbuff(httrack.path_html,argv[na],(int) (a-argv[na]));
  569.                   strcatbuff(httrack.path_log,a+1);
  570.                 } else {
  571.                   strcpybuff(httrack.path_log,argv[na]);
  572.                   strcpybuff(httrack.path_html,argv[na]);
  573.                 }
  574.                 // Eliminer les cas comme -O "C:\mirror\"
  575.                 if (httrack.path_log[0]=='"') {  // Guillemets
  576.                   char tmp[256];
  577.                   strcpybuff(tmp,httrack.path_log+1);
  578.                   if (tmp[strlen(tmp)-1]=='"')
  579.                     tmp[strlen(tmp)-1]='\0';
  580.                   strcpybuff(httrack.path_log,tmp);
  581.                 }
  582.                 if (httrack.path_html[0]=='"') {
  583.                   char tmp[256];
  584.                   strcpybuff(tmp,httrack.path_html+1);
  585.                   if (tmp[strlen(tmp)-1]=='"')
  586.                     tmp[strlen(tmp)-1]='\0';
  587.                   strcpybuff(httrack.path_html,tmp);
  588.                 }
  589.                 check_path(httrack.path_log,argv_firsturl);
  590.                 if (check_path(httrack.path_html,argv_firsturl)) {
  591.                   httrack.dir_topindex=1;     // rebuilt top index
  592.                 }
  593.                 
  594.                 //printf("-->%s\n%s\n",httrack.path_html,httrack.path_log);
  595.                 
  596.               }
  597.               break;
  598.             }  // switch
  599.             com++;    
  600.           }  // while
  601.           
  602.         }  // arg
  603.         
  604.       }  // for
  605.      
  606.          /* if doit.log exists, or if new URL(s) defined, 
  607.       then DO NOT load standard config files */
  608.       /* (config files are added in doit.log) */
  609. #if DEBUG_STEPS
  610.       printf("Loading httrackrc/doit.log\n");
  611. #endif
  612.       /* recreate a doit.log (no old doit.log or new URLs (and parameters)) */
  613.       if ((strnotempty(httrack.path_log)) || (strnotempty(httrack.path_html)))
  614.         loops++;      // do not loop once again and do not include rc file (O option exists)
  615.       else {
  616.         if ( (!fexist(fconcat(httrack.path_log,"hts-cache/doit.log"))) || (argv_url>0) ) {
  617.           if (!optinclude_file(fconcat(httrack.path_log,HTS_HTTRACKRC),&argc,argv,x_argvblk,&x_ptr))
  618.             if (!optinclude_file(HTS_HTTRACKRC,&argc,argv,x_argvblk,&x_ptr)) {
  619.               if (!optinclude_file(fconcat(hts_gethome(),"/"HTS_HTTRACKRC),&argc,argv,x_argvblk,&x_ptr)) {
  620. #ifdef HTS_HTTRACKCNF
  621.                 optinclude_file(HTS_HTTRACKCNF,&argc,argv,x_argvblk,&x_ptr);
  622. #endif
  623.               }
  624.             }
  625.         } else
  626.           loops++;      // do not loop once again
  627.       }
  628.  
  629.       loops++;
  630.    } // while
  631.  
  632.   }  // traiter -O
  633.  
  634.   /* load doit.log and insert in current command line */
  635.   if ( fexist(fconcat(httrack.path_log,"hts-cache/doit.log")) && (argv_url<=0) ) {
  636.     FILE* fp=fopen(fconcat(httrack.path_log,"hts-cache/doit.log"),"rb");
  637.     if (fp) {
  638.       int insert_after=1;     /* insΘrer aprΦs nom au dΘbut */
  639.       //
  640.       char buff[8192];
  641.       char *p,*lastp;
  642.       linput(fp,buff,8000);
  643.       fclose(fp); fp=NULL;
  644.       p=buff;
  645.       do {
  646.         int insert_after_argc;
  647.         // read next
  648.         lastp=p;
  649.         if (p) {
  650.           p=next_token(p,1);
  651.           if (p) {
  652.             *p=0;    // null
  653.             p++;
  654.           }
  655.         }
  656.  
  657.         /* Insert parameters BUT so that they can be in the same order */
  658.         if (lastp) {
  659.           if (strnotempty(lastp)) {
  660.             //char* argv0;
  661.             //int len;
  662.             insert_after_argc=argc-insert_after;
  663.             //argv0 = (argv+insert_after)[0];
  664.             cmdl_ins(lastp,insert_after_argc,(argv+insert_after),x_argvblk,x_ptr);
  665.             /*
  666.             DONE IN 'next_token'
  667.             len = strlen(argv0);
  668.             if (len >= 2 && argv0[0]=='\"' && argv0[len-1]=='\"') {   // "foo"
  669.               char tempo[1024];
  670.               tempo[0] = '\0';
  671.               strncatbuff(tempo, argv0+1, len-2);
  672.               strcpybuff(argv0, tempo);
  673.             }
  674.             */
  675.             argc=insert_after_argc+insert_after;
  676.             insert_after++;
  677.           }
  678.         }
  679.       } while(lastp!=NULL);
  680.       //fclose(fp);
  681.     }
  682.   }
  683.  
  684.  
  685.   // Existence d'un cache - pas de new mais un old.. renommer
  686. #if DEBUG_STEPS
  687.   printf("Checking cache\n");
  688. #endif
  689.   if (!fexist(fconcat(httrack.path_log,"hts-cache/new.zip"))) {
  690.     if ( fexist(fconcat(httrack.path_log,"hts-cache/old.zip")) ) {
  691.       rename(fconcat(httrack.path_log,"hts-cache/old.zip"),fconcat(httrack.path_log,"hts-cache/new.zip"));
  692.     }
  693.   } else if ( (!fexist(fconcat(httrack.path_log,"hts-cache/new.dat"))) || (!fexist(fconcat(httrack.path_log,"hts-cache/new.ndx"))) ) {
  694.     if ( (fexist(fconcat(httrack.path_log,"hts-cache/old.dat"))) && (fexist(fconcat(httrack.path_log,"hts-cache/old.ndx"))) ) {
  695.       remove(fconcat(httrack.path_log,"hts-cache/new.dat"));
  696.       remove(fconcat(httrack.path_log,"hts-cache/new.ndx"));
  697.       //remove(fconcat(httrack.path_log,"hts-cache/new.lst"));
  698.       rename(fconcat(httrack.path_log,"hts-cache/old.dat"),fconcat(httrack.path_log,"hts-cache/new.dat"));
  699.       rename(fconcat(httrack.path_log,"hts-cache/old.ndx"),fconcat(httrack.path_log,"hts-cache/new.ndx"));
  700.       //rename(fconcat(httrack.path_log,"hts-cache/old.lst"),fconcat(httrack.path_log,"hts-cache/new.lst"));
  701.     }
  702.   }
  703.  
  704.   /* Interrupted mirror detected */
  705.   if (!httrack.quiet) {
  706.     if (fexist(fconcat(httrack.path_log,"hts-in_progress.lock"))) {
  707.       /* Old cache */
  708.       if ( (fexist(fconcat(httrack.path_log,"hts-cache/old.dat"))) && (fexist(fconcat(httrack.path_log,"hts-cache/old.ndx"))) ) {
  709.         if (httrack.log != NULL) {
  710.           fprintf(httrack.log,"Warning!\n");
  711.           fprintf(httrack.log,"An aborted mirror has been detected!\nThe current temporary cache is required for any update operation and only contains data downloaded during the last aborted session.\nThe former cache might contain more complete information; if you do not want to lose that information, you have to restore it and delete the current cache.\nThis can easily be done here by erasing the hts-cache/new.* files\n");
  712.           fprintf(httrack.log,"Please restart HTTrack with --continue (-iC1) option to override this message!\n");
  713.         }
  714.         exit(0);
  715.       }
  716.     }
  717.   }
  718.     
  719.   // remplacer "macros" comme --spider
  720.   // permet de lancer httrack sans a avoir α se rappeler de syntaxes comme p0C0I0Qc32 ..
  721. #if DEBUG_STEPS
  722.   printf("Checking last macros\n");
  723. #endif
  724.   {
  725.     int i;
  726.     for(i=0;i<argc;i++) {
  727. #if DEBUG_STEPS
  728.       printf("Checking #%d:\n",argv[i]);
  729.       printf("%s\n",argv[i]);
  730. #endif
  731.       if (argv[i][0]=='-') {
  732.         if (argv[i][1]=='-') {  // --xxx
  733.           if ((strfield2(argv[i]+2,"clean")) || (strfield2(argv[i]+2,"tide"))) {  // nettoyer
  734.             strcpybuff(argv[i]+1,"");
  735.             if (fexist(fconcat(httrack.path_log,"hts-log.txt")))
  736.               remove(fconcat(httrack.path_log,"hts-log.txt"));
  737.             if (fexist(fconcat(httrack.path_log,"hts-err.txt")))
  738.               remove(fconcat(httrack.path_log,"hts-err.txt"));
  739.             if (fexist(fconcat(httrack.path_html,"index.html")))
  740.               remove(fconcat(httrack.path_html,"index.html"));
  741.             /* */
  742.             if (fexist(fconcat(httrack.path_log,"hts-cache/new.zip")))
  743.               remove(fconcat(httrack.path_log,"hts-cache/new.zip"));
  744.             if (fexist(fconcat(httrack.path_log,"hts-cache/old.zip")))
  745.               remove(fconcat(httrack.path_log,"hts-cache/old.zip"));
  746.             if (fexist(fconcat(httrack.path_log,"hts-cache/new.dat")))
  747.               remove(fconcat(httrack.path_log,"hts-cache/new.dat"));
  748.             if (fexist(fconcat(httrack.path_log,"hts-cache/new.ndx")))
  749.               remove(fconcat(httrack.path_log,"hts-cache/new.ndx"));
  750.             if (fexist(fconcat(httrack.path_log,"hts-cache/old.dat")))
  751.               remove(fconcat(httrack.path_log,"hts-cache/old.dat"));
  752.             if (fexist(fconcat(httrack.path_log,"hts-cache/old.ndx")))
  753.               remove(fconcat(httrack.path_log,"hts-cache/old.ndx"));
  754.             if (fexist(fconcat(httrack.path_log,"hts-cache/new.lst")))
  755.               remove(fconcat(httrack.path_log,"hts-cache/new.lst"));
  756.             if (fexist(fconcat(httrack.path_log,"hts-cache/old.lst")))
  757.               remove(fconcat(httrack.path_log,"hts-cache/old.lst"));
  758.             if (fexist(fconcat(httrack.path_log,"hts-cache/new.txt")))
  759.               remove(fconcat(httrack.path_log,"hts-cache/new.txt"));
  760.             if (fexist(fconcat(httrack.path_log,"hts-cache/old.txt")))
  761.               remove(fconcat(httrack.path_log,"hts-cache/old.txt"));
  762.             if (fexist(fconcat(httrack.path_log,"hts-cache/doit.log")))
  763.               remove(fconcat(httrack.path_log,"hts-cache/doit.log"));
  764.             if (fexist(fconcat(httrack.path_log,"hts-in_progress.lock")))
  765.               remove(fconcat(httrack.path_log,"hts-in_progress.lock"));
  766.             rmdir(fconcat(httrack.path_log,"hts-cache"));
  767.             //
  768.           } else if (strfield2(argv[i]+2,"catchurl")) {      // capture d'URL via proxy temporaire!
  769.             argv_url=1;     // forcer a passer les parametres
  770.             strcpybuff(argv[i]+1,"#P");
  771.             //
  772.           } else if (strfield2(argv[i]+2,"updatehttrack")) {
  773. #ifdef _WIN32
  774.             char s[HTS_CDLMAXSIZE];
  775.             sprintf(s,"%s not available in this version",argv[i]);
  776.             HTS_PANIC_PRINTF(s);
  777.             htsmain_free();
  778.             return -1;
  779. #else
  780. #if 0
  781.             char _args[8][256];
  782.             char *args[8];
  783.             
  784.             printf("Cheking for updates...\n");
  785.             strcpybuff(_args[0],argv[0]);
  786.             strcpybuff(_args[1],"--get");
  787.             sprintf(_args[2],HTS_UPDATE_WEBSITE,HTS_PLATFORM,"");
  788.             strcpybuff(_args[3],"--quickinfo");
  789.             args[0]=_args[0];
  790.             args[1]=_args[1];
  791.             args[2]=_args[2];
  792.             args[3]=_args[3];
  793.             args[4]=NULL;
  794.             if (execvp(args[0],args)==-1) {
  795.             }
  796. #endif
  797. #endif
  798.           }
  799.           //
  800.           else {
  801.             char s[HTS_CDLMAXSIZE];
  802.             sprintf(s,"%s not recognized",argv[i]);
  803.             HTS_PANIC_PRINTF(s);
  804.             htsmain_free();
  805.             return -1;
  806.           }
  807.  
  808.         } 
  809.       }
  810.     }
  811.   }
  812.  
  813.   // Compter urls/jokers
  814.   /*
  815.   if (argv_url<=0) { 
  816.     int na;
  817.     argv_url=0;
  818.     for(na=1;na<argc;na++) {
  819.       if ( (strcmp(argv[na],"-P")==0) || (strcmp(argv[na],"-N")==0) || (strcmp(argv[na],"-F")==0) || (strcmp(argv[na],"-O")==0) || (strcmp(argv[na],"-V")==0) ) {
  820.         na++;    // sauter nom de proxy
  821.       } else if (!cmdl_opt(argv[na])) { 
  822.         argv_url++;   // un de plus       
  823.       } else if (strcmp(argv[na],"-h")==0) {
  824.         help(argv[0],!httrack.quiet);
  825.         htsmain_free();
  826.         return 0;
  827.       } else {
  828.         if ((strchr(argv[na],'q')!=NULL))
  829.           httrack.quiet=1;    // ne pas poser de questions! (nohup par exemple)
  830.         if ((strchr(argv[na],'i')!=NULL)) {  // doit.log!
  831.           argv_url=0;
  832.           na=argc;
  833.         }
  834.       }
  835.     }
  836.   }  
  837.   */
  838.  
  839.   // Ici on ajoute les arguments qui ont ΘtΘ appelΘs avant au cas o∙ on rΘcupΦre une session
  840.   // Exemple: httrack www.truc.fr -L0 puis ^C puis httrack sans URL : ajouter URL prΘcΘdente
  841.   /*
  842.   if (argv_url==0) {
  843.     //if ((fexist(fconcat(httrack.path_log,"hts-cache/new.dat"))) && (fexist(fconcat(httrack.path_log,"hts-cache/new.ndx")))) {  // il existe dΘja un cache prΘcΘdent.. renommer
  844.     if (fexist(fconcat(httrack.path_log,"hts-cache/doit.log"))) {    // un cache est prΘsent
  845.       
  846.       x_argvblk=(char*) calloct(32768,1);
  847.       
  848.       if (x_argvblk!=NULL) {
  849.         FILE* fp;
  850.         int x_argc;
  851.         
  852.         //strcpybuff(x_argvblk,"httrack ");
  853.         fp=fopen(fconcat(httrack.path_log,"hts-cache/doit.log"),"rb");
  854.         if (fp) {
  855.           linput(fp,x_argvblk+strlen(x_argvblk),8192);
  856.           fclose(fp); fp=NULL;
  857.         }
  858.         
  859.         // calculer arguments selon derniers arguments
  860.         x_argv[0]=argv[0];
  861.         x_argc=1;
  862.         {
  863.           char* p=x_argvblk;
  864.           do {
  865.             x_argv[x_argc++]=p;
  866.             //p=strstr(p," ");
  867.             // exemple de chaine: "echo \"test\"" c:\a "\$0"
  868.             p=next_token(p,1);    // prochain token
  869.             if (p) {
  870.               *p=0;    // octet nul (tableau)
  871.               p++;
  872.             }            
  873.           } while(p!=NULL);
  874.         }
  875.         // recopier arguments actuels (pointeurs uniquement)
  876.         {
  877.           int na;
  878.           for(na=1;na<argc;na++) {
  879.             if (strcmp(argv[na],"-O") != 0)    // SAUF le path!
  880.               x_argv[x_argc++]=argv[na];
  881.             else
  882.               na++;
  883.           }
  884.         }
  885.         argc=x_argc;      // nouvel argc
  886.         argv=x_argv;      // nouvel argv
  887.       }
  888.       
  889.       
  890.     }
  891.     //}
  892.   }
  893.   */
  894.   
  895.   // VΘrifier quiet
  896.   /*
  897.   { 
  898.     int na;    
  899.     for(na=1;na<argc;na++) {
  900.       if (!cmdl_opt(argv[na])) { 
  901.         if ((strcmp(argv[na],"-P")==0) || (strcmp(argv[na],"-N")==0) || (strcmp(argv[na],"-F")==0) || (strcmp(argv[na],"-O")==0) || (strcmp(argv[na],"-V")==0))
  902.           na++;    // sauter nom de proxy
  903.       } else {
  904.         if ((strchr(argv[na],'q')!=NULL) || (strchr(argv[na],'i')!=NULL))
  905.           httrack.quiet=1;    // ne pas poser de questions! (nohup par exemple)
  906.       }
  907.     }
  908.   }
  909.   */
  910.  
  911.   // Pas d'URL
  912. #if DEBUG_STEPS
  913.   printf("Checking URLs\n");
  914. #endif
  915.   if (argv_url==0) {
  916.     // PrΘsence d'un cache, que faire?..
  917.     if (
  918.       ( fexist(fconcat(httrack.path_log,"hts-cache/new.zip")) )
  919.       ||
  920.       ( fexist(fconcat(httrack.path_log,"hts-cache/new.dat")) && fexist(fconcat(httrack.path_log,"hts-cache/new.ndx")) )
  921.       ) {  // il existe dΘja un cache prΘcΘdent.. renommer
  922.       if (fexist(fconcat(httrack.path_log,"hts-cache/doit.log"))) {    // un cache est prΘsent
  923.         if (x_argvblk!=NULL) {
  924.           int m;        
  925.           // Θtablir mode - mode cache: 1 (cache valide) 2 (cache α vΘrifier)
  926.           if (fexist(fconcat(httrack.path_log,"hts-in_progress.lock"))) {    // cache prioritaire
  927.             m=1;
  928.             recuperer=1;
  929.           } else {
  930.             m=2;
  931.           }
  932.           httrack.cache=m;
  933.           
  934.           if (httrack.quiet==0) {  // sinon on continue automatiquement
  935.             HT_REQUEST_START;
  936.             HT_PRINT("A cache (hts-cache/) has been found in the directory ");
  937.             HT_PRINT(httrack.path_log);
  938.             HT_PRINT(LF);
  939.             if (m==1) {
  940.               HT_PRINT("That means that a transfer has been aborted"LF);
  941.               HT_PRINT("OK to Continue ");
  942.             } else {
  943.               HT_PRINT("That means you can update faster the remote site(s)"LF);
  944.               HT_PRINT("OK to Update ");
  945.             }
  946.             HT_PRINT("httrack "); HT_PRINT(x_argvblk); HT_PRINT("?"LF);
  947.             HT_REQUEST_END;
  948.             if (!ask_continue()) { 
  949.               htsmain_free();
  950.               return 0;
  951.             }
  952.           }
  953.           
  954.         } else {
  955.           HTS_PANIC_PRINTF("Error, not enough memory");
  956.           htsmain_free();
  957.           return -1;
  958.         }
  959.       } else { // log existe pas
  960.         HTS_PANIC_PRINTF("A cache has been found, but no command line");
  961.         printf("Please launch httrack with proper parameters to reuse the cache\n");
  962.         htsmain_free();
  963.         return -1;
  964.       }
  965.       
  966.     } else {    // aucune URL dΘfinie et pas de cache
  967.       if (argc > 1 && strcmp(argv[0], "-#h") == 0) {
  968.         printf("HTTrack version "HTTRACK_VERSION"%s\n", WHAT_is_available);
  969.         exit(0);
  970.       }
  971. #if HTS_ANALYSTE!=2
  972.       if (httrack.quiet) {
  973. #endif
  974.         help(argv[0],!httrack.quiet);
  975.         htsmain_free();
  976.         return -1;
  977. #if HTS_ANALYSTE!=2
  978.       } else {
  979.         help_wizard(&httrack);
  980.         htsmain_free();
  981.         return -1;
  982.       }
  983. #endif
  984.       htsmain_free();
  985.       return 0;
  986.     }
  987.   } else {   // plus de 2 paramΦtres
  988.     // un fichier log existe?
  989.     if (fexist(fconcat(httrack.path_log,"hts-in_progress.lock"))) {  // fichier lock?
  990.       //char s[32];
  991.       
  992.       httrack.cache=1;    // cache prioritaire
  993.       if (httrack.quiet==0) {
  994.         if (
  995.           ( fexist(fconcat(httrack.path_log,"hts-cache/new.zip")) )
  996.           ||
  997.           ( fexist(fconcat(httrack.path_log,"hts-cache/new.dat")) && fexist(fconcat(httrack.path_log,"hts-cache/new.ndx")) )
  998.           ) {
  999.           HT_REQUEST_START;
  1000.           HT_PRINT("There is a lock-file in the directory ");
  1001.           HT_PRINT(httrack.path_log);
  1002.           HT_PRINT(LF"That means that a mirror has not been terminated"LF);
  1003.           HT_PRINT("Be sure you call httrack with proper parameters"LF);
  1004.           HT_PRINT("(The cache allows you to restart faster the transfer)"LF);
  1005.           HT_REQUEST_END;
  1006.           if (!ask_continue()) {
  1007.             htsmain_free();
  1008.             return 0;
  1009.           }
  1010.         }
  1011.       }
  1012.     } else if (fexist(fconcat(httrack.path_html,"index.html"))) {
  1013.       //char s[32];
  1014.       httrack.cache=2;  // cache vient aprΦs test de validitΘ
  1015.       if (httrack.quiet==0) {
  1016.         if (
  1017.           ( fexist(fconcat(httrack.path_log,"hts-cache/new.zip")) )
  1018.           ||
  1019.           ( fexist(fconcat(httrack.path_log,"hts-cache/new.dat")) && fexist(fconcat(httrack.path_log,"hts-cache/new.ndx")) )
  1020.           ) {
  1021.           HT_REQUEST_START;
  1022.           HT_PRINT("There is an index.html and a hts-cache folder in the directory ");
  1023.           HT_PRINT(httrack.path_log);
  1024.           HT_PRINT(LF"A site may have been mirrored here, that could mean that you want to update it"LF);
  1025.           HT_PRINT("Be sure parameters are ok"LF);
  1026.           HT_REQUEST_END;
  1027.           if (!ask_continue()) {
  1028.             htsmain_free();
  1029.             return 0;
  1030.           }
  1031.         } else {
  1032.           HT_REQUEST_START;
  1033.           HT_PRINT("There is an index.html in the directory ");
  1034.           HT_PRINT(httrack.path_log);
  1035.           HT_PRINT(" but no cache"LF);
  1036.           HT_PRINT("There is an index.html in the directory, but no cache"LF);
  1037.           HT_PRINT("A site may have been mirrored here, and erased.."LF);
  1038.           HT_PRINT("Be sure parameters are ok"LF);
  1039.           HT_REQUEST_END;
  1040.           if (!ask_continue()) {
  1041.             htsmain_free();
  1042.             return 0;
  1043.           }
  1044.         }
  1045.       }
  1046.     }
  1047.   }
  1048.   
  1049.   
  1050.   // Treat parameters
  1051.   // Traiter les paramΦtres
  1052. #if DEBUG_STEPS
  1053.   printf("Analyze parameters\n");
  1054. #endif
  1055.   { 
  1056.     char* com;
  1057.     int na;
  1058.     
  1059.     for(na=1;na<argc;na++) {
  1060.  
  1061.       if (argv[na][0]=='"') {
  1062.         char tempo[HTS_CDLMAXSIZE];
  1063.         strcpybuff(tempo,argv[na]+1);
  1064.         if (tempo[strlen(tempo)-1]!='"') {
  1065.           char s[HTS_CDLMAXSIZE];
  1066.           sprintf(s,"Missing quote in %s",argv[na]);
  1067.           HTS_PANIC_PRINTF(s);
  1068.           htsmain_free();
  1069.           return -1;
  1070.         }
  1071.         tempo[strlen(tempo)-1]='\0';
  1072.         strcpybuff(argv[na],tempo);
  1073.       }
  1074.  
  1075.       if (cmdl_opt(argv[na])) { // option
  1076.         com=argv[na]+1;
  1077.         
  1078.         while(*com) {
  1079.           switch(*com) {
  1080.           case ' ': case 9: case '-': case '\0': break;
  1081.             //
  1082.           case 'h': 
  1083.             help(argv[0],0); 
  1084.             htsmain_free();
  1085.             return 0;   // dΘja fait normalement
  1086.             //
  1087.           case 'g':    // rΘcupΘrer un (ou plusieurs) fichiers isolΘs
  1088.             httrack.wizard=2;             // le wizard on peut plus s'en passer..
  1089.             //httrack.wizard=0;             // pas de wizard
  1090.             httrack.cache=0;              // ni de cache
  1091.             httrack.makeindex=0;          // ni d'index
  1092.             httrack_logmode=1;            // erreurs α l'Θcran
  1093.             httrack.savename_type=1003;   // mettre dans le rΘpertoire courant
  1094.             httrack.depth=0;              // ne pas explorer la page
  1095.             httrack.accept_cookie=0;      // pas de cookies
  1096.             httrack.robots=0;             // pas de robots
  1097.             break;
  1098.           case 'w': httrack.wizard=2;    // wizard 'soft' (ne pose pas de questions)
  1099.             httrack.travel=0;
  1100.             httrack.seeker=1;
  1101.             break;
  1102.           case 'W': httrack.wizard=1;    // Wizard-Help (pose des questions)
  1103.             httrack.travel=0;
  1104.             httrack.seeker=1;
  1105.             break;
  1106.           case 'r':                      // n'est plus le recurse get bestial mais wizard itou!
  1107.             if (isdigit((unsigned char)*(com+1))) {
  1108.               sscanf(com+1,"%d",&httrack.depth);
  1109.               while(isdigit((unsigned char)*(com+1))) com++;
  1110.             } else httrack.depth=3;
  1111.             break;
  1112. /*
  1113.           case 'r': httrack.wizard=0;
  1114.             if (isdigit((unsigned char)*(com+1))) {
  1115.               sscanf(com+1,"%d",&httrack.depth);
  1116.               while(isdigit((unsigned char)*(com+1))) com++;
  1117.             } else httrack.depth=3;
  1118.             break;
  1119. */
  1120.             //
  1121.             // note: les tests httrack.depth sont pour Θviter de faire
  1122.             // un miroir du web (:-O) accidentelement ;-)
  1123.           case 'a': /*if (httrack.depth==9999) httrack.depth=3;*/
  1124.             httrack.travel=0+(httrack.travel&256); break;
  1125.           case 'd': /*if (httrack.depth==9999) httrack.depth=3;*/
  1126.             httrack.travel=1+(httrack.travel&256); break;
  1127.           case 'l': /*if (httrack.depth==9999) httrack.depth=3;*/
  1128.             httrack.travel=2+(httrack.travel&256); break;
  1129.           case 'e': /*if (httrack.depth==9999) httrack.depth=3;*/
  1130.             httrack.travel=7+(httrack.travel&256); break;
  1131.           case 't': httrack.travel|=256; break;
  1132.           case 'n': httrack.nearlink=1; break;
  1133.           case 'x': httrack.external=1; break;
  1134.             //
  1135.           case 'U': httrack.seeker=2; break;
  1136.           case 'D': httrack.seeker=1; break;
  1137.           case 'S': httrack.seeker=0; break;
  1138.           case 'B': httrack.seeker=3; break;
  1139.             //
  1140.           case 'Y': httrack.mirror_first_page=1; break;
  1141.             //
  1142.           case 'q': case 'i': httrack.quiet=1; break;
  1143.             //
  1144.           case 'Q': httrack_logmode=0; break;
  1145.           case 'v': httrack_logmode=1; break;
  1146.           case 'f': httrack_logmode=2; if (*(com+1)=='2') httrack_logmode=3; while(isdigit((unsigned char)*(com+1))) com++; break;
  1147.             //
  1148.           //case 'A': httrack.urlmode=1; break;
  1149.           //case 'R': httrack.urlmode=2; break;
  1150.           case 'K': httrack.urlmode=0; 
  1151.             if (isdigit((unsigned char)*(com+1))) {
  1152.               sscanf(com+1,"%d",&httrack.urlmode);
  1153.               if (httrack.urlmode == 0) {  // in fact K0 ==> K2
  1154.                                            // and K ==> K0
  1155.                 httrack.urlmode=2;
  1156.               }
  1157.               while(isdigit((unsigned char)*(com+1))) com++; 
  1158.             }
  1159.             //if (*(com+1)=='0') { httrack.urlmode=2; com++; } break;
  1160.             //
  1161.           case 'c':
  1162.             if (isdigit((unsigned char)*(com+1))) {
  1163.               sscanf(com+1,"%d",&httrack.maxsoc);
  1164.               while(isdigit((unsigned char)*(com+1))) com++;
  1165.               httrack.maxsoc=max(httrack.maxsoc,1);     // FORCER A 1
  1166.             } else httrack.maxsoc=4;
  1167.             
  1168.             break;
  1169.             //
  1170.           case 'p': sscanf(com+1,"%d",&httrack.getmode); while(isdigit((unsigned char)*(com+1))) com++; break;
  1171.             //        
  1172.           case 'G': sscanf(com+1,LLintP,&httrack.fragment); while(isdigit((unsigned char)*(com+1))) com++; break;
  1173.           case 'M': sscanf(com+1,LLintP,&httrack.maxsite); while(isdigit((unsigned char)*(com+1))) com++; break;
  1174.           case 'm': sscanf(com+1,LLintP,&httrack.maxfile_nonhtml); while(isdigit((unsigned char)*(com+1))) com++; 
  1175.             if (*(com+1)==',') {
  1176.               com++;
  1177.               sscanf(com+1,LLintP,&httrack.maxfile_html); while(isdigit((unsigned char)*(com+1))) com++;
  1178.             } else httrack.maxfile_html=-1;
  1179.             break;
  1180.             //
  1181.           case 'T': sscanf(com+1,"%d",&httrack.timeout); while(isdigit((unsigned char)*(com+1))) com++; break;
  1182.           case 'J': sscanf(com+1,"%d",&httrack.rateout); while(isdigit((unsigned char)*(com+1))) com++; break;
  1183.           case 'R': sscanf(com+1,"%d",&httrack.retry); while(isdigit((unsigned char)*(com+1))) com++; break;
  1184.           case 'E': sscanf(com+1,"%d",&httrack.maxtime); while(isdigit((unsigned char)*(com+1))) com++; break;
  1185.           case 'H': sscanf(com+1,"%d",&httrack.hostcontrol); while(isdigit((unsigned char)*(com+1))) com++; break;
  1186.           case 'A': sscanf(com+1,"%d",&httrack.maxrate); while(isdigit((unsigned char)*(com+1))) com++; break;
  1187.  
  1188.           case 'j': httrack.parsejava=1; if (*(com+1)=='0') { httrack.parsejava=0; com++; } break;
  1189.             //
  1190.           case 'I': httrack.makeindex=1; if (*(com+1)=='0') { httrack.makeindex=0; com++; } break;
  1191.             //
  1192.           case 'X': httrack.delete_old=1; if (*(com+1)=='0') { httrack.delete_old=0; com++; } break;
  1193.             //
  1194.           case 'b': sscanf(com+1,"%d",&httrack.accept_cookie); while(isdigit((unsigned char)*(com+1))) com++; break;
  1195.             //
  1196.           case 'N':
  1197.             if (strcmp(argv[na],"-N")==0) {    // Tout seul
  1198.               if ((na+1>=argc) || (argv[na+1][0]=='-')) {  // erreur
  1199.                 HTS_PANIC_PRINTF("Option N needs a number, or needs to be followed by a blank space, and a string");
  1200.                 printf("Example: -N4\n");
  1201.                 htsmain_free();
  1202.                 return -1;
  1203.               } else {
  1204.                 na++;
  1205.                 if (strlen(argv[na])>=127) {
  1206.                   HTS_PANIC_PRINTF("Userdef structure string too long");
  1207.                   htsmain_free();
  1208.                   return -1;
  1209.                 }
  1210.                 strcpybuff(httrack.savename_userdef,argv[na]);
  1211.                 if (strnotempty(httrack.savename_userdef))
  1212.                   httrack.savename_type = -1;    // userdef!
  1213.                 else
  1214.                   httrack.savename_type = 0;    // -N "" : par dΘfaut
  1215.               }
  1216.             } else {
  1217.               sscanf(com+1,"%d",&httrack.savename_type); while(isdigit((unsigned char)*(com+1))) com++;
  1218.             }
  1219.             break;
  1220.           case 'L': 
  1221.             {
  1222.               sscanf(com+1,"%d",&httrack.savename_83); 
  1223.               switch(httrack.savename_83) {
  1224.               case 0:
  1225.                 httrack.savename_83=1;
  1226.                 break;
  1227.               case 1:
  1228.                 httrack.savename_83=0;
  1229.                 break;
  1230.               default:
  1231.                 httrack.savename_83=2;
  1232.                 break;
  1233.               }
  1234.               while(isdigit((unsigned char)*(com+1))) com++; 
  1235.             }
  1236.             break;
  1237.           case 's': 
  1238.             if (isdigit((unsigned char)*(com+1))) {
  1239.               sscanf(com+1,"%d",&httrack.robots);
  1240.               while(isdigit((unsigned char)*(com+1))) com++;
  1241.             } else httrack.robots=1;
  1242. #if DEBUG_ROBOTS
  1243.             printf("robots.txt mode set to %d\n",httrack.robots);
  1244. #endif
  1245.             break;
  1246.           case 'o': sscanf(com+1,"%d",&httrack.errpage); while(isdigit((unsigned char)*(com+1))) com++; break;
  1247.           case 'u': sscanf(com+1,"%d",&httrack.check_type); while(isdigit((unsigned char)*(com+1))) com++; break;
  1248.             //
  1249.           case 'C': 
  1250.             if (isdigit((unsigned char)*(com+1))) {
  1251.               sscanf(com+1,"%d",&httrack.cache);
  1252.               while(isdigit((unsigned char)*(com+1))) com++;
  1253.             } else httrack.cache=1;
  1254.             break;
  1255.           case 'k': httrack.all_in_cache=1; break;
  1256.             //
  1257.           case 'z': httrack.debug=1; break;  // petit debug
  1258.           case 'Z': httrack.debug=2; break;  // GROS debug
  1259.             //
  1260.           case '&': case '%': {    // deuxiΦme jeu d'options
  1261.             com++;
  1262.             switch(*com) {
  1263.             case 'M': httrack.mimehtml = 1; if (*(com+1)=='0') { httrack.mimehtml=0; com++; } break;
  1264.             case 'k': httrack.nokeepalive = 0; if (*(com+1)=='0') { httrack.nokeepalive = 1; com++; } break;
  1265.             case 'x': httrack.passprivacy=1; if (*(com+1)=='0') { httrack.passprivacy=0; com++; } break;   // No passwords in html files
  1266.             case 'q': httrack.includequery=1; if (*(com+1)=='0') { httrack.includequery=0; com++; } break;   // No passwords in html files
  1267.             case 'I': httrack.kindex=1; if (isdigit((unsigned char)*(com+1))) { sscanf(com+1,"%d",&httrack.kindex); while(isdigit((unsigned char)*(com+1))) com++; }
  1268.               break;    // Keyword Index
  1269.             case 'c': sscanf(com+1,"%d",&httrack.maxconn); while(isdigit((unsigned char)*(com+1))) com++; break;
  1270.             case 'e': sscanf(com+1,"%d",&httrack.extdepth); while(isdigit((unsigned char)*(com+1))) com++; break;
  1271.             case 'B': httrack.tolerant=1; if (*(com+1)=='0') { httrack.tolerant=0; com++; } break;   // HTTP/1.0 notamment
  1272.             case 'h': httrack.http10=1; if (*(com+1)=='0') { httrack.http10=0; com++; } break;   // HTTP/1.0
  1273.             case 'z': httrack.nocompression=1; if (*(com+1)=='0') { httrack.nocompression=0; com++; } break;   // pas de compression
  1274.             case 'f': httrack.ftp_proxy=1; if (*(com+1)=='0') { httrack.ftp_proxy=0; com++; } break;   // proxy http pour ftp
  1275.             case 'P': httrack.parseall=1; if (*(com+1)=='0') { httrack.parseall=0; com++; } break;   // tout parser
  1276.             case 'n': httrack.norecatch=1; if (*(com+1)=='0') { httrack.norecatch=0; com++; } break;   // ne pas reprendre fichiers effacΘs localement
  1277.             case 's': httrack.sizehack=1; if (*(com+1)=='0') { httrack.sizehack=0; com++; } break;   // hack sur content-length
  1278.             case 'u': httrack.urlhack=1; if (*(com+1)=='0') { httrack.urlhack=0; com++; } break;   // url hack
  1279.             case 'v': httrack.verbosedisplay=2; if (isdigit((unsigned char)*(com+1))) { sscanf(com+1,"%d",&httrack.verbosedisplay); while(isdigit((unsigned char)*(com+1))) com++; } break;
  1280.             case 'i': httrack.dir_topindex = 1; if (*(com+1)=='0') { httrack.dir_topindex=0; com++; } break;
  1281.  
  1282.             // preserve: no footer, original links
  1283.             case 'p':
  1284.               httrack.footer[0]='\0';
  1285.               httrack.urlmode=4;
  1286.               break;
  1287.             case 'L':    // URL list
  1288.               if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1289.                 HTS_PANIC_PRINTF("Option %L needs to be followed by a blank space, and a text filename");
  1290.                 printf("Example: -%%L \"mylist.txt\"\n");
  1291.                 htsmain_free();
  1292.                 return -1;
  1293.               } else{
  1294.                 na++;
  1295.                 if (strlen(argv[na])>=254) {
  1296.                   HTS_PANIC_PRINTF("File list string too long");
  1297.                   htsmain_free();
  1298.                   return -1;
  1299.                 }
  1300.                 strcpybuff(httrack.filelist,argv[na]);
  1301.               }
  1302.               break;
  1303.             case 'b':  // bind
  1304.               if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1305.                 HTS_PANIC_PRINTF("Option %b needs to be followed by a blank space, and a local hostname");
  1306.                 printf("Example: -%%b \"ip4.localhost\"\n");
  1307.                 htsmain_free();
  1308.                 return -1;
  1309.               } else{
  1310.                 na++;
  1311.                 if (strlen(argv[na])>=254) {
  1312.                   HTS_PANIC_PRINTF("Hostname string too long");
  1313.                   htsmain_free();
  1314.                   return -1;
  1315.                 }
  1316.                 strcpybuff(httrack.proxy.bindhost,argv[na]);
  1317.               }
  1318.               break;
  1319.             case 'S':    // Scan Rules list
  1320.               if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1321.                 HTS_PANIC_PRINTF("Option %S needs to be followed by a blank space, and a text filename");
  1322.                 printf("Example: -%%S \"myfilterlist.txt\"\n");
  1323.                 htsmain_free();
  1324.                 return -1;
  1325.               } else{
  1326.                 INTsys fz;
  1327.                 na++;
  1328.                 fz = fsize(argv[na]);
  1329.                 if (fz < 0) {
  1330.                   HTS_PANIC_PRINTF("File url list could not be opened");
  1331.                   htsmain_free();
  1332.                   return -1;
  1333.                 } else {
  1334.                   FILE* fp = fopen(argv[na], "rb");
  1335.                   if (fp != NULL) {
  1336.                     int cl = (int) strlen(url);
  1337.                     ensureUrlCapacity(url, url_sz, cl + fz + 8192);
  1338.                     if ((INTsys)fread(url + cl, 1, fz, fp) != fz) {
  1339.                       HTS_PANIC_PRINTF("File url list could not be read");
  1340.                       htsmain_free();
  1341.                       return -1;
  1342.                     }
  1343.                     fclose(fp);
  1344.                     *(url + cl + fz) = '\0';
  1345.                   }
  1346.                 }
  1347.               }
  1348.               break;
  1349.             case 'A':    // assume
  1350.               if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1351.                 HTS_PANIC_PRINTF("Option %A needs to be followed by a blank space, and a filesystemtype=mimetype/mimesubtype parameters");
  1352.                 printf("Example: -%%A php3=text/html,asp=text/html\n");
  1353.                 htsmain_free();
  1354.                 return -1;
  1355.               } else{
  1356.                 char* a;
  1357.                 na++;
  1358.                 if ( (strlen(argv[na]) + strlen(httrack.mimedefs) + 4) >= sizeof(httrack.mimedefs)) {
  1359.                   HTS_PANIC_PRINTF("Mime definition string too long");
  1360.                   htsmain_free();
  1361.                   return -1;
  1362.                 }
  1363.                 // --assume standard
  1364.                 if (strcmp(argv[na],"standard") == 0) {
  1365.                   strcpybuff(httrack.mimedefs,"\n");
  1366.                   strcatbuff(httrack.mimedefs,HTS_ASSUME_STANDARD);
  1367.                   strcatbuff(httrack.mimedefs,"\n");
  1368.                 } else {
  1369.                   strcatbuff(httrack.mimedefs,argv[na]);
  1370.                   strcatbuff(httrack.mimedefs,"\n");
  1371.                 }
  1372.                 a=httrack.mimedefs;
  1373.                 while(*a) {
  1374.                   switch(*a) {
  1375.                   case ',': case ' ': case '\r': case ';': case '\t':
  1376.                     *a='\n';
  1377.                     break;
  1378.                   }
  1379.                   a++;
  1380.                 }
  1381.               }
  1382.               break;
  1383.               //
  1384.             case 'l': 
  1385.               if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1386.                 HTS_PANIC_PRINTF("Option %l needs to be followed by a blank space, and an ISO language code");
  1387.                 printf("Example: -%%l \"en\"\n");
  1388.                 htsmain_free();
  1389.                 return -1;
  1390.               } else{
  1391.                 na++;
  1392.                 if (strlen(argv[na])>=62) {
  1393.                   HTS_PANIC_PRINTF("Lang list string too long");
  1394.                   htsmain_free();
  1395.                   return -1;
  1396.                 }
  1397.                 strcpybuff(httrack.lang_iso,argv[na]);
  1398.               }
  1399.               break;
  1400.               //
  1401.             case 'F':     // footer id
  1402.               if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1403.                 HTS_PANIC_PRINTF("Option %F needs to be followed by a blank space, and a footer string");
  1404.                 printf("Example: -%%F \"<!-- Mirrored from %%s by HTTrack Website Copier/"HTTRACK_AFF_VERSION" "HTTRACK_AFF_AUTHORS", %%s -->\"\n");
  1405.                 htsmain_free();
  1406.                 return -1;
  1407.               } else{
  1408.                 na++;
  1409.                 if (strlen(argv[na])>=254) {
  1410.                   HTS_PANIC_PRINTF("Footer string too long");
  1411.                   htsmain_free();
  1412.                   return -1;
  1413.                 }
  1414.                 strcpybuff(httrack.footer,argv[na]);
  1415.               }
  1416.               break;
  1417.             case 'H':                 // debug headers
  1418.               _DEBUG_HEAD=1;
  1419.               break;
  1420.             case 'O':
  1421. #if HTS_WIN
  1422.               printf("Warning option -%%O has no effect in this system (chroot)\n");
  1423. #else
  1424.               switch_chroot=1;
  1425. #endif
  1426.               break;
  1427.             case 'U':                 // setuid
  1428.               if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1429.                 HTS_PANIC_PRINTF("Option %U needs to be followed by a blank space, and a username");
  1430.                 printf("Example: -%%U smith\n");
  1431.                 htsmain_free();
  1432.                 return -1;
  1433.               } else {
  1434.                 na++;
  1435. #if HTS_WIN
  1436.                 printf("Warning option -%%U has no effect on this system (setuid)\n");
  1437. #else
  1438. #ifndef HTS_DO_NOT_USE_UID
  1439.                 /* Change the user id and gid */
  1440.                 {
  1441.                   struct passwd* userdef=getpwnam((const char*)argv[na]);
  1442.                   if (userdef) {    /* we'll have to switch the user id */
  1443.                     switch_gid=userdef->pw_gid;
  1444.                     switch_uid=userdef->pw_uid;
  1445.                   }
  1446.                 }
  1447. #else
  1448.                 printf("Warning option -%%U has no effect with this compiled version (setuid)\n");
  1449. #endif
  1450. #endif
  1451.               }
  1452.               break;
  1453.               
  1454.             case 'W':       // Wrapper callback
  1455.               // --wrapper check-link=obj.so:check_link
  1456.               if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1457.                 HTS_PANIC_PRINTF("Option %W needs to be followed by a blank space, and a <callback-name>=<myfile.so>:<function-name> field");
  1458.                 printf("Example: -%%W check-link=checklink.so:check\n");
  1459.                 htsmain_free();
  1460.                 return -1;
  1461.               } else {
  1462.                 char callbackname[128];
  1463.                 char* a = argv[na + 1];
  1464.                 char* pos = strchr(a, '=');
  1465.                 na++;
  1466.                 if (pos != NULL && (pos - a) > 0 && (pos - a + 2) < sizeof(callbackname)) {
  1467.                   char* posf = strchr(pos + 1, ':');
  1468.                   char filename[1024];
  1469.                   callbackname[0] = '\0';
  1470.                   strncatbuff(callbackname, a, pos - a);
  1471.                   pos++;
  1472.                   if (posf != NULL && (posf - pos) > 0 && (posf - pos + 2) < sizeof(filename)) {
  1473.                     void* userfunction;
  1474.                     filename[0] = '\0';
  1475.                     strncatbuff(filename, pos, posf - pos);
  1476.                     posf++;
  1477.                     userfunction = getFunctionPtr(&httrack, filename, posf);
  1478.                     if (userfunction != NULL) {
  1479.                       if ((void*)htswrap_read(callbackname) != NULL) {
  1480.                         if (htswrap_add(callbackname, userfunction)) {
  1481.                           set_wrappers();        /* Re-read wrappers internal static functions */
  1482.                           if ((void*)htswrap_read(callbackname) == userfunction) {
  1483.                             if (!httrack.quiet) {
  1484.                               fprintf(stderr, "successfully plugged [%s -> %s:%s]\n", callbackname, posf, filename);
  1485.                             }
  1486.                           } else {
  1487.                             char tmp[1024 * 2];
  1488.                             sprintf(tmp, "option %%W : unable to (re)plug the function %s from the file %s for the callback %s", posf, filename, callbackname);
  1489.                             HTS_PANIC_PRINTF(tmp);
  1490.                             htsmain_free();
  1491.                             return -1;
  1492.                           }
  1493.                         } else {
  1494.                           char tmp[1024 * 2];
  1495.                           sprintf(tmp, "option %%W : unable to plug the function %s from the file %s for the callback %s", posf, filename, callbackname);
  1496.                           HTS_PANIC_PRINTF(tmp);
  1497.                           htsmain_free();
  1498.                           return -1;
  1499.                         }
  1500.                       } else {
  1501.                         char tmp[1024 * 2];
  1502.                         sprintf(tmp, "option %%W : unknown or undefined callback %s", callbackname);
  1503.                         HTS_PANIC_PRINTF(tmp);
  1504.                         htsmain_free();
  1505.                         return -1;
  1506.                       }
  1507.                     } else {
  1508.                       char tmp[1024 * 2];
  1509.                       sprintf(tmp, "option %%W : unable to load the function %s in the file %s for the callback %s", posf, filename, callbackname);
  1510.                       HTS_PANIC_PRINTF(tmp);
  1511.                       htsmain_free();
  1512.                       return -1;
  1513.                     }
  1514.                   } else {
  1515.                     HTS_PANIC_PRINTF("Syntax error in option %W : filename error : this function needs to be followed by a blank space, and a <callback-name>=<myfile.so>:<function-name> field");
  1516.                     printf("Example: -%%W check-link=checklink.so:check\n");
  1517.                     htsmain_free();
  1518.                     return -1;
  1519.                   }
  1520.                 } else {
  1521.                   HTS_PANIC_PRINTF("Syntax error in option %W : this function needs to be followed by a blank space, and a <callback-name>=<myfile.so>:<function-name> field");
  1522.                   printf("Example: -%%W check-link=checklink.so:check\n");
  1523.                   htsmain_free();
  1524.                   return -1;
  1525.                 }
  1526.               }
  1527.               break;
  1528.               
  1529.             case 'R':    // Referer
  1530.               if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1531.                 HTS_PANIC_PRINTF("Option %R needs to be followed by a blank space, and a referer URL");
  1532.                 printf("Example: -%%R \"http://www.example.com/\"\n");
  1533.                 htsmain_free();
  1534.                 return -1;
  1535.               } else{
  1536.                 na++;
  1537.                 if (strlen(argv[na])>=254) {
  1538.                   HTS_PANIC_PRINTF("Referer URL too long");
  1539.                   htsmain_free();
  1540.                   return -1;
  1541.                 }
  1542.                 strcpybuff(httrack.referer, argv[na]);
  1543.               }
  1544.               break;
  1545.             case 'E':    // From Email address
  1546.               if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1547.                 HTS_PANIC_PRINTF("Option %E needs to be followed by a blank space, and an email");
  1548.                 printf("Example: -%%E \"postmaster@example.com\"\n");
  1549.                 htsmain_free();
  1550.                 return -1;
  1551.               } else{
  1552.                 na++;
  1553.                 if (strlen(argv[na])>=254) {
  1554.                   HTS_PANIC_PRINTF("From email too long");
  1555.                   htsmain_free();
  1556.                   return -1;
  1557.                 }
  1558.                 strcpybuff(httrack.from, argv[na]);
  1559.               }
  1560.               break;
  1561.  
  1562.             default: {
  1563.               char s[HTS_CDLMAXSIZE];
  1564.               sprintf(s,"invalid option %%%c\n",*com);
  1565.               HTS_PANIC_PRINTF(s);
  1566.               htsmain_free();
  1567.               return -1;
  1568.                      }
  1569.               break;
  1570.               
  1571.             }
  1572.                     }
  1573.             break;
  1574.             //
  1575.           case '@': {    // troisiΦme jeu d'options
  1576.             com++;
  1577.             switch(*com) {
  1578.             case 'i': 
  1579. #if HTS_INET6==0
  1580.               printf("Warning, option @i has no effect (v6 routines not compiled)\n");
  1581. #else 
  1582.               {
  1583.                 int res=0;
  1584.                 if (isdigit((unsigned char)*(com+1))) {
  1585.                   sscanf(com+1,"%d",&res); while(isdigit((unsigned char)*(com+1))) com++; 
  1586.                 }
  1587.                 switch(res) {
  1588.                 case 1:
  1589.                 case 4:
  1590.                   IPV6_resolver=1;
  1591.                   break;
  1592.                 case 2:
  1593.                 case 6:
  1594.                   IPV6_resolver=2;
  1595.                   break;
  1596.                 case 0:
  1597.                   IPV6_resolver=0;
  1598.                   break;
  1599.                 default:
  1600.                   printf("Unknown flag @i%d\n", res);
  1601.                   htsmain_free();
  1602.                   return -1;
  1603.                   break;
  1604.                 }
  1605.               }
  1606. #endif
  1607.               break;
  1608.               
  1609.                 default: {
  1610.                   char s[HTS_CDLMAXSIZE];
  1611.                   sprintf(s,"invalid option %%%c\n",*com);
  1612.                   HTS_PANIC_PRINTF(s);
  1613.                   htsmain_free();
  1614.                   return -1;
  1615.                          }
  1616.                   break;
  1617.                   
  1618.                   //case 's': httrack.sslengine=1; if (isdigit((unsigned char)*(com+1))) { sscanf(com+1,"%d",&httrack.sslengine); while(isdigit((unsigned char)*(com+1))) com++; } break;
  1619.             }
  1620.                     }
  1621.             break;
  1622.             
  1623.             //
  1624.           case '#':  { // non documentΘ
  1625.             com++;
  1626.             switch(*com) {
  1627.             case 'C':   // list cache files : httrack -#C '*spid*.gif' will attempt to find the matching file
  1628.               {
  1629.                 int hasFilter = 0;
  1630.                 int found = 0;
  1631.                 char* filter=NULL;
  1632.                 cache_back cache;
  1633.                 inthash cache_hashtable=inthash_new(HTS_HASH_SIZE);
  1634.                 int backupXFR = htsMemoryFastXfr;
  1635.                 int sendb = 0;
  1636.                 if (isdigit((unsigned char)*(com+1))) {
  1637.                   sscanf(com+1,"%d",&sendb);
  1638.                   while(isdigit((unsigned char)*(com+1))) com++;
  1639.                 } else sendb=0;
  1640.                 if (!((na+1>=argc) || (argv[na+1][0]=='-'))) {
  1641.                   na++;
  1642.                   hasFilter = 1;
  1643.                   filter=argv[na];
  1644.                 }
  1645.                 htsMemoryFastXfr = 1;               /* fast load */
  1646.  
  1647.                 memset(&cache, 0, sizeof(cache_back));
  1648.                 cache.type=1;       // cache?
  1649.                 cache.log=stdout;     // log?
  1650.                 cache.errlog=stderr;  // err log?
  1651.                 cache.ptr_ant=cache.ptr_last=0;   // pointeur pour anticiper
  1652.                 cache.hashtable=(void*)cache_hashtable;      /* copy backcache hash */
  1653.                 cache.ro = 1;          /* read only */
  1654.                 if (cache.hashtable) {
  1655.                   char adr[HTS_URLMAXSIZE*2];
  1656.                   char fil[HTS_URLMAXSIZE*2];
  1657.                   char url[HTS_URLMAXSIZE*2];
  1658.                   char linepos[256];
  1659.                   int  pos;
  1660.                   char* cacheNdx = readfile(fconcat(httrack.path_log,"hts-cache/new.ndx"));
  1661.                   cache_init(&cache,&httrack);            /* load cache */
  1662.                   if (cacheNdx != NULL) {
  1663.                     char firstline[256];
  1664.                     char* a = cacheNdx;
  1665.                     a+=cache_brstr(a, firstline);
  1666.                     a+=cache_brstr(a, firstline);
  1667.                     while ( a != NULL ) {
  1668.                       a=strchr(a+1,'\n');     /* start of line */
  1669.                       if (a) {
  1670.                         htsblk r;
  1671.                         /* */
  1672.                         a++;
  1673.                         /* read "host/file" */
  1674.                         a+=binput(a,adr,HTS_URLMAXSIZE);
  1675.                         a+=binput(a,fil,HTS_URLMAXSIZE);
  1676.                         url[0]='\0';
  1677.                         if (!link_has_authority(adr))
  1678.                           strcatbuff(url, "http://");
  1679.                         strcatbuff(url, adr);
  1680.                         strcatbuff(url, fil);
  1681.                         /* read position */
  1682.                         a+=binput(a,linepos,200);
  1683.                         sscanf(linepos,"%d",&pos);
  1684.                         if (!hasFilter
  1685.                           ||
  1686.                           (strjoker(url, filter, NULL, NULL) != NULL)
  1687.                           ) {
  1688.                           r = cache_read_ro(&httrack, &cache, adr, fil, "", NULL);    // lire entrΘe cache + data
  1689.                           if (r.statuscode != -1) {    // No errors
  1690.                             found++;
  1691.                             if (!hasFilter) {
  1692.                               fprintf(stdout, "%s%s%s\r\n", 
  1693.                                 (link_has_authority(adr)) ? "" : "http://", 
  1694.                                 adr, fil);
  1695.                             } else {
  1696.                               char msg[256], cdate[256];
  1697.                               char sav[HTS_URLMAXSIZE*2];
  1698.                               infostatuscode(msg, r.statuscode);
  1699.                               time_gmt_rfc822(cdate);
  1700.  
  1701.                               fprintf(stdout, "HTTP/1.1 %d %s\r\n",
  1702.                                 r.statuscode,
  1703.                                 r.msg[0] ? r.msg : msg
  1704.                                 );
  1705.                               fprintf(stdout, "X-Host: %s\r\n", adr);
  1706.                               fprintf(stdout, "X-File: %s\r\n", fil);
  1707.                               fprintf(stdout, "X-URL: %s%s%s\r\n", 
  1708.                                 (link_has_authority(adr)) ? "" : "http://", 
  1709.                                 adr, fil);
  1710.                               if (url_savename(adr, fil, sav, NULL, NULL, NULL, NULL,
  1711.                                 &httrack, NULL, 0, NULL, 0, &cache, NULL, 0, 0)!=-1) {
  1712.                                 if (fexist(sav)) {
  1713.                                   fprintf(stdout, "Content-location: %s\r\n", sav);
  1714.                                 }
  1715.                               }
  1716.                               fprintf(stdout, "Date: %s\r\n", cdate);
  1717.                               fprintf(stdout, "Server: HTTrack Website Copier/"HTTRACK_VERSION"\r\n");
  1718.                               if (r.lastmodified[0]) {
  1719.                                 fprintf(stdout, "Last-Modified: %s\r\n", r.lastmodified);
  1720.                               }
  1721.                               if (r.etag[0]) {
  1722.                                 fprintf(stdout, "Etag: %s\r\n", r.etag);
  1723.                               }
  1724.                               if (r.totalsize >= 0) {
  1725.                                 fprintf(stdout, "Content-Length: "LLintP"\r\n", r.totalsize);
  1726.                               }
  1727.                               fprintf(stdout, "X-Content-Length: "LLintP"\r\n", (r.size >= 0) ? r.size : (-r.size) );
  1728.                               if (r.contenttype >= 0) {
  1729.                                 fprintf(stdout, "Content-Type: %s\r\n", r.contenttype);
  1730.                               }
  1731.                               if (r.cdispo[0]) {
  1732.                                 fprintf(stdout, "Content-Disposition: %s\r\n", r.cdispo);
  1733.                               }
  1734.                               if (r.contentencoding[0]) {
  1735.                                 fprintf(stdout, "Content-Encoding: %s\r\n", r.contentencoding);
  1736.                               }
  1737.                               if (r.is_chunk) {
  1738.                                 fprintf(stdout, "Transfer-Encoding: chunked\r\n");
  1739.                               }
  1740. #if HTS_USEOPENSSL
  1741.                               if (r.ssl) {
  1742.                                 fprintf(stdout, "X-SSL: yes\r\n");
  1743.                               }
  1744. #endif
  1745.                               if (r.is_write) {
  1746.                                 fprintf(stdout, "X-Direct-To-Disk: yes\r\n");
  1747.                               }
  1748.                               if (r.compressed) {
  1749.                                 fprintf(stdout, "X-Compressed: yes\r\n");
  1750.                               }
  1751.                               if (r.notmodified) {
  1752.                                 fprintf(stdout, "X-Not-Modified: yes\r\n");
  1753.                               }
  1754.                               if (r.is_chunk) {
  1755.                                 fprintf(stdout, "X-Chunked: yes\r\n");
  1756.                               }
  1757.                               fprintf(stdout, "\r\n");
  1758.                               /* Send the body */
  1759.                               if (sendb && r.adr) {
  1760.                                 fprintf(stdout, "%s\r\n", r.adr);
  1761.                               }
  1762.                             }
  1763.                           }
  1764.                         }
  1765.                       }
  1766.                     }
  1767.                     freet(cacheNdx);
  1768.                   }
  1769.                 }
  1770.                 if (!found) {
  1771.                   fprintf(stderr, "No cache entry found%s%s%s\r\n",
  1772.                     (hasFilter)?" for '":"",
  1773.                     (hasFilter)?filter:"",
  1774.                     (hasFilter)?"'":""
  1775.                     );
  1776.                 }
  1777.                 htsMemoryFastXfr = backupXFR;
  1778.                 return 0;
  1779.               }
  1780.               break;
  1781.             case 'E':     // extract cache
  1782.               if (!hts_extract_meta(httrack.path_log)) {
  1783.                 fprintf(stderr, "* error extracting meta-data\n");
  1784.                 return 1;
  1785.               }
  1786.               fprintf(stderr, "* successfully extracted meta-data\n");
  1787.               return 0;
  1788.               break;
  1789.             case 'X': 
  1790. #ifndef STRDEBUG
  1791.               fprintf(stderr, "warning: no string debugging support built, option has no effect\n");
  1792. #endif
  1793.               htsMemoryFastXfr=1; 
  1794.               if (*(com+1)=='0') { htsMemoryFastXfr=0; com++; } 
  1795.               break;
  1796.             case 'R':
  1797.               {
  1798.                 char* name;
  1799.                 uLong repaired = 0;
  1800.                 uLong repairedBytes = 0;
  1801.                 if (fexist(fconcat(httrack.path_log,"hts-cache/new.zip"))) {
  1802.                   name = fconcat(httrack.path_log,"hts-cache/new.zip");
  1803.                 } else if (fexist(fconcat(httrack.path_log,"hts-cache/old.zip"))) {
  1804.                   name = fconcat(httrack.path_log,"hts-cache/old.zip");
  1805.                 } else {
  1806.                   fprintf(stderr, "* error: no cache found in %s\n", fconcat(httrack.path_log,"hts-cache/new.zip"));
  1807.                   return 1;
  1808.                 }
  1809.                 fprintf(stderr, "Cache: trying to repair %s\n", name);
  1810.                 if (unzRepair(name, 
  1811.                   fconcat(httrack.path_log,"hts-cache/repair.zip"),
  1812.                   fconcat(httrack.path_log,"hts-cache/repair.tmp"),
  1813.                   &repaired, &repairedBytes
  1814.                   ) == Z_OK) {
  1815.                   unlink(name);
  1816.                   rename(fconcat(httrack.path_log,"hts-cache/repair.zip"), name);
  1817.                   fprintf(stderr,"Cache: %d bytes successfully recovered in %d entries\n", (int) repairedBytes, (int) repaired);
  1818.                 } else {
  1819.                   fprintf(stderr, "Cache: could not repair the cache\n");
  1820.                 }
  1821.               }
  1822.               return 0;
  1823.               break;
  1824.             case '~': /* internal lib test */
  1825.               {
  1826.                 char thisIsATestYouShouldSeeAnError[12];
  1827.                 strcpybuff(thisIsATestYouShouldSeeAnError, "0123456789012345678901234567890123456789");
  1828.                 return 0;
  1829.               }
  1830.               break;
  1831.             case 'f': httrack.flush=1; break;
  1832.             case 'h':
  1833.               printf("HTTrack version "HTTRACK_VERSION"%s\n", WHAT_is_available);
  1834.               return 0;
  1835.               break;
  1836.             case 'p': /* httrack.aff_progress=1; deprecated */ break;
  1837.             case 'S': httrack.shell=1; break;  // stdin sur un shell
  1838.             case 'K': httrack.keyboard=1; break;  // vΘrifier stdin
  1839.               //
  1840.             case 'L': sscanf(com+1,"%d",&httrack.maxlink); while(isdigit((unsigned char)*(com+1))) com++; break;
  1841.             case 'F': sscanf(com+1,"%d",&httrack.maxfilter); while(isdigit((unsigned char)*(com+1))) com++; break;
  1842.             case 'Z': httrack.makestat=1; break;
  1843.             case 'T': httrack.maketrack=1; break;
  1844.             case 'u': sscanf(com+1,"%d",&httrack.waittime); while(isdigit((unsigned char)*(com+1))) com++; break;
  1845.  
  1846.             /*case 'R':    // ohh ftp, catch->ftpget
  1847.               HTS_PANIC_PRINTF("Unexpected internal error with -#R command");
  1848.               htsmain_free();
  1849.               return -1;        
  1850.               break;
  1851.               */
  1852.             case 'P': {     // catchurl
  1853.               help_catchurl(httrack.path_log);
  1854.               htsmain_free();
  1855.               return 0;
  1856.                       }
  1857.               break;
  1858.           
  1859.             case '0':   /* test #0 : filters */
  1860.               if (na+2>=argc) {
  1861.                 HTS_PANIC_PRINTF("Option #0 needs to be followed by a filter string and a string");
  1862.                 printf("Example: '-#0' '*.gif' 'foo.gif'\n");
  1863.                 htsmain_free();
  1864.                 return -1;
  1865.               } else {
  1866.                 if (strjoker(argv[na+2],argv[na+1],NULL,NULL))
  1867.                   printf("%s does match %s\n",argv[na+2],argv[na+1]);
  1868.                 else
  1869.                   printf("%s does NOT match %s\n",argv[na+2],argv[na+1]);
  1870.                 htsmain_free();
  1871.                 return 0;
  1872.               }
  1873.               break;
  1874.             case '!':
  1875.               if (na+1>=argc) {
  1876.                 HTS_PANIC_PRINTF("Option #! needs to be followed by a commandline");
  1877.                 printf("Example: '-#!' 'echo hello'\n");
  1878.                 htsmain_free();
  1879.                 return -1;
  1880.               } else {
  1881.                 system(argv[na+1]);
  1882.               }
  1883.               break;
  1884.             case 'd':
  1885.               httrack.parsedebug = 1;
  1886.               break;
  1887.  
  1888.             /* autotest */
  1889.             case 't':     /* not yet implemented */
  1890.               fprintf(stderr, "** AUTOCHECK OK\n");
  1891.               exit(0);
  1892.               break;
  1893.  
  1894.             default: printf("Internal option %c not recognized\n",*com); break;
  1895.             }
  1896.                      }
  1897.             break; 
  1898.           case 'O':    // output path
  1899.             na++;     // sauter, dΘja traitΘ
  1900.             break;
  1901.           case 'P':    // proxy
  1902.             if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1903.               HTS_PANIC_PRINTF("Option P needs to be followed by a blank space, and a proxy proxy:port or user:id@proxy:port");
  1904.               printf("Example: -P proxy.myhost.com:8080\n");
  1905.               htsmain_free();
  1906.               return -1;
  1907.             } else {
  1908.               char* a;
  1909.               na++;
  1910.               httrack.proxy.active=1;
  1911.               // Rechercher MAIS en partant de la fin α cause de user:pass@proxy:port
  1912.               a = argv[na] + strlen(argv[na]) -1;
  1913.               // a=strstr(argv[na],":");  // port
  1914.               while( (a > argv[na]) && (*a != ':') && (*a != '@') ) a--;
  1915.               if (*a == ':') {  // un port est prΘsent, <proxy>:port
  1916.                 sscanf(a+1,"%d",&httrack.proxy.port);
  1917.                 httrack.proxy.name[0]='\0';
  1918.                 strncatbuff(httrack.proxy.name,argv[na],(int) (a - argv[na]));
  1919.               } else {  // <proxy>
  1920.                 httrack.proxy.port=8080;
  1921.                 strcpybuff(httrack.proxy.name,argv[na]);
  1922.               }
  1923.             }
  1924.             break;
  1925.           case 'F':    // user-agent field
  1926.             if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1927.               HTS_PANIC_PRINTF("Option F needs to be followed by a blank space, and a user-agent name");
  1928.               printf("Example: -F \"my_user_agent/1.0\"\n");
  1929.               htsmain_free();
  1930.               return -1;
  1931.             } else{
  1932.               na++;
  1933.               if (strlen(argv[na])>=126) {
  1934.                 HTS_PANIC_PRINTF("User-agent length too long");
  1935.                 htsmain_free();
  1936.                 return -1;
  1937.               }
  1938.               strcpybuff(httrack.user_agent,argv[na]);
  1939.               if (strnotempty(httrack.user_agent))
  1940.                 httrack.user_agent_send=1;
  1941.               else
  1942.                 httrack.user_agent_send=0;    // -F "" dΘsactive l'option
  1943.             }
  1944.             break;
  1945.             //
  1946.           case 'V':    // execute command
  1947.             if ((na+1>=argc) || (argv[na+1][0]=='-')) {
  1948.               HTS_PANIC_PRINTF("Option V needs to be followed by a system-command string");
  1949.               printf("Example: -V \"tar uvf some.tar \\$0\"\n");
  1950.               htsmain_free();
  1951.               return -1;
  1952.             } else{
  1953.               na++;
  1954.               if (strlen(argv[na])>=2048) {
  1955.                 HTS_PANIC_PRINTF("System-command length too long");
  1956.                 htsmain_free();
  1957.                 return -1;
  1958.               }
  1959.               strcpybuff(httrack.sys_com,argv[na]);
  1960.               if (strnotempty(httrack.sys_com))
  1961.                 httrack.sys_com_exec=1;
  1962.               else
  1963.                 httrack.sys_com_exec=0;    // -V "" dΘsactive l'option
  1964.             }
  1965.             break;
  1966.             //
  1967.           default: {
  1968.             char s[HTS_CDLMAXSIZE];
  1969.             sprintf(s,"invalid option %c\n",*com);
  1970.             HTS_PANIC_PRINTF(s);
  1971.             htsmain_free();
  1972.             return -1;
  1973.                    }
  1974.             break;
  1975.           }  // switch
  1976.           com++;    
  1977.         }  // while
  1978.         
  1979.       }  else {  // URL/filters
  1980.         char tempo[1024];       
  1981.         if (strnotempty(url)) strcatbuff(url," ");  // espace de sΘparation
  1982.         strcpybuff(tempo,unescape_http_unharm(argv[na],1));
  1983.         escape_spc_url(tempo);
  1984.         strcatbuff(url,tempo);
  1985.       }  // if argv=- etc. 
  1986.       
  1987.     }  // for
  1988.   }
  1989.   
  1990. #if BDEBUG==3  
  1991.   printf("URLs/filters=%s\n",url);
  1992. #endif
  1993.  
  1994. #if DEBUG_STEPS
  1995.   printf("Analyzing parameters done\n");
  1996. #endif
  1997.  
  1998.  
  1999. #if HTS_WIN
  2000. #else
  2001. #ifndef HTS_DO_NOT_USE_UID
  2002.   /* Chroot - xxc */
  2003.   if (switch_chroot) {
  2004.     uid_t userid=getuid();
  2005.     //struct passwd* userdef=getpwuid(userid);
  2006.     //if (userdef) {
  2007.     if (!userid) {
  2008.       //if (strcmp(userdef->pw_name,"root")==0) {
  2009.       char rpath[1024];
  2010.       //printf("html=%s log=%s\n",httrack.path_html,httrack.path_log);    // xxc
  2011.       if ((httrack.path_html[0]) && (httrack.path_log[0])) {
  2012.         char *a=httrack.path_html,*b=httrack.path_log,*c=NULL,*d=NULL;
  2013.         c=a; d=b;
  2014.         while ((*a) && (*a == *b)) {
  2015.           if (*a=='/') { c=a; d=b; }
  2016.           a++;
  2017.           b++;
  2018.         }
  2019.  
  2020.         rpath[0]='\0';
  2021.         if (c != httrack.path_html) {
  2022.           if (httrack.path_html[0]!='/')
  2023.             strcatbuff(rpath,"./");
  2024.           strncatbuff(rpath,httrack.path_html,(int) (c - httrack.path_html));
  2025.         }
  2026.         {
  2027.           char tmp[1024];
  2028.           strcpybuff(tmp,c); strcpybuff(httrack.path_html,tmp);
  2029.           strcpybuff(tmp,d); strcpybuff(httrack.path_log,tmp);
  2030.         }
  2031.       } else {
  2032.         strcpybuff(rpath,"./");
  2033.         strcpybuff(httrack.path_html,"/");
  2034.         strcpybuff(httrack.path_log,"/");
  2035.       }
  2036.       if (rpath[0]) {
  2037.         printf("[changing root path to %s (path_data=%s,path_log=%s)]\n",rpath,httrack.path_html,httrack.path_log);
  2038.         if (chroot(rpath)) {
  2039.           printf("ERROR! Can not chroot to %s!\n",rpath);
  2040.           return -1;
  2041.         }
  2042.         if (chdir("/")) {     /* new root */
  2043.           printf("ERROR! Can not chdir to %s!\n",rpath);
  2044.           return -1;
  2045.         }
  2046.       } else
  2047.         printf("WARNING: chroot not possible with these paths\n");
  2048.     }
  2049.     //}
  2050.   }
  2051.  
  2052.   /* Setuid */
  2053.   if (switch_uid>=0) {
  2054.     printf("[setting user/group to %d/%d]\n",switch_uid,switch_gid);
  2055.     if (setgid(switch_gid))
  2056.       printf("WARNING! Can not setgid to %d!\n",switch_gid);
  2057.     if (setuid(switch_uid))
  2058.       printf("WARNING! Can not setuid to %d!\n",switch_uid);
  2059.   }
  2060.  
  2061.   /* Final check */
  2062.   {
  2063.     uid_t userid=getuid();
  2064.     if (!userid) {              /* running as r00t */
  2065.       printf("WARNING! You are running this program as root!\n");
  2066.       printf("It might be a good idea to use the -%%U option to change the userid:\n");
  2067.       printf("Example: -%%U smith\n\n");
  2068.     }
  2069.   }
  2070. #endif
  2071. #endif
  2072.   
  2073.   //printf("WARNING! This is *only* a beta-release of HTTrack\n");
  2074.   io_flush;
  2075.   
  2076. #if DEBUG_STEPS
  2077.   printf("Cache & log settings\n");
  2078. #endif
  2079.   
  2080.   // on utilise le cache..
  2081.   // en cas de prΘsence des deux versions, garder la version la plus avancΘe,
  2082.   // cad la version contenant le plus de fichiers  
  2083.   if (httrack.cache) {
  2084.     if (fexist(fconcat(httrack.path_log,"hts-in_progress.lock"))) {   // problemes..
  2085.       if ( fexist(fconcat(httrack.path_log,"hts-cache/new.dat")) ) { 
  2086.         if ( fexist(fconcat(httrack.path_log,"hts-cache/old.zip")) ) {
  2087.           if (fsize(fconcat(httrack.path_log,"hts-cache/new.zip"))<32768) {
  2088.             if (fsize(fconcat(httrack.path_log,"hts-cache/old.zip"))>65536) {
  2089.               if (fsize(fconcat(httrack.path_log,"hts-cache/old.zip")) > fsize(fconcat(httrack.path_log,"hts-cache/new.zip"))) {
  2090.                 remove(fconcat(httrack.path_log,"hts-cache/new.zip"));
  2091.                 rename(fconcat(httrack.path_log,"hts-cache/old.zip"), fconcat(httrack.path_log,"hts-cache/new.zip"));
  2092.               }
  2093.             }
  2094.           }
  2095.         }
  2096.       }
  2097.       else if (fexist(fconcat(httrack.path_log,"hts-cache/new.dat")) && fexist(fconcat(httrack.path_log,"hts-cache/new.ndx"))) { 
  2098.         if (fexist(fconcat(httrack.path_log,"hts-cache/old.dat")) && fexist(fconcat(httrack.path_log,"hts-cache/old.ndx"))) {
  2099.           // switcher si new<32Ko et old>65Ko (tailles arbitraires) ?
  2100.           // ce cas est peut Ωtre une erreur ou un crash d'un miroir ancien, prendre
  2101.           // alors l'ancien cache
  2102.           if (fsize(fconcat(httrack.path_log,"hts-cache/new.dat"))<32768) {
  2103.             if (fsize(fconcat(httrack.path_log,"hts-cache/old.dat"))>65536) {
  2104.               if (fsize(fconcat(httrack.path_log,"hts-cache/old.dat")) > fsize(fconcat(httrack.path_log,"hts-cache/new.dat"))) {
  2105.                 remove(fconcat(httrack.path_log,"hts-cache/new.dat"));
  2106.                 remove(fconcat(httrack.path_log,"hts-cache/new.ndx"));
  2107.                 rename(fconcat(httrack.path_log,"hts-cache/old.dat"),fconcat(httrack.path_log,"hts-cache/new.dat"));
  2108.                 rename(fconcat(httrack.path_log,"hts-cache/old.ndx"),fconcat(httrack.path_log,"hts-cache/new.ndx"));  
  2109.                 //} else {  // ne rien faire
  2110.                 //  remove("hts-cache/old.dat");
  2111.                 //  remove("hts-cache/old.ndx");
  2112.               }
  2113.             }
  2114.           }
  2115.         }
  2116.       }
  2117.     }
  2118.   }
  2119.  
  2120.   // DΘbuggage des en tΩtes
  2121.   if (_DEBUG_HEAD) {
  2122.     ioinfo=fopen(fconcat(httrack.path_log,"hts-ioinfo.txt"),"wb");
  2123.   }
  2124.   
  2125.   {
  2126.     char n_lock[256];
  2127.     // on peut pas avoir un affichage ET un fichier log
  2128.     // ca sera pour la version 2
  2129.     if (httrack_logmode==1) {
  2130.       httrack.log=stdout;
  2131.       httrack.errlog=stderr;
  2132.     } else if (httrack_logmode>=2) {
  2133.       // deux fichiers log
  2134.       structcheck(httrack.path_log);
  2135.       if (fexist(fconcat(httrack.path_log,"hts-log.txt")))
  2136.         remove(fconcat(httrack.path_log,"hts-log.txt"));
  2137.       if (fexist(fconcat(httrack.path_log,"hts-err.txt")))
  2138.         remove(fconcat(httrack.path_log,"hts-err.txt"));
  2139.  
  2140.       /* Check FS directory structure created */
  2141.       structcheck(httrack.path_log);
  2142.  
  2143.       httrack.log=fopen(fconcat(httrack.path_log,"hts-log.txt"),"w");
  2144.       if (httrack_logmode==2)
  2145.         httrack.errlog=fopen(fconcat(httrack.path_log,"hts-err.txt"),"w");
  2146.       else
  2147.         httrack.errlog=httrack.log;
  2148.       if (httrack.log==NULL) {
  2149.         char s[HTS_CDLMAXSIZE];
  2150.         sprintf(s,"Unable to create log file %s",fconcat(httrack.path_log,"hts-log.txt"));
  2151.         HTS_PANIC_PRINTF(s);
  2152.         htsmain_free();
  2153.         return -1;
  2154.       } else if (httrack.errlog==NULL) {
  2155.         char s[HTS_CDLMAXSIZE];
  2156.         sprintf(s,"Unable to create log file %s",fconcat(httrack.path_log,"hts-err.txt"));
  2157.         HTS_PANIC_PRINTF(s);
  2158.         htsmain_free();
  2159.         return -1;
  2160.       }
  2161.  
  2162.     } else {
  2163.       httrack.log=NULL;
  2164.       httrack.errlog=NULL;
  2165.     }
  2166.     
  2167.     // un petit lock-file pour indiquer un miroir en cours, ainsi qu'un Θventuel fichier log
  2168.     {
  2169.       FILE* fp=NULL;
  2170.       //int n=0;
  2171.       char t[256];
  2172.       time_local_rfc822(t);    // faut bien que ca serve quelque part l'heure RFC1945 arf'
  2173.       
  2174.       /* readme for information purpose */
  2175.       {
  2176.         FILE* fp=fopen(fconcat(httrack.path_log,"hts-cache/readme.txt"),"wb");
  2177.         if (fp) {
  2178.           fprintf(fp,"What's in this folder?"LF);
  2179.           fprintf(fp,""LF);
  2180.           fprintf(fp,"This folder (hts-cache) has been generated by WinHTTrack "HTTRACK_VERSION"%s"LF, WHAT_is_available);
  2181.           fprintf(fp,"and is used for updating this website."LF);
  2182.           fprintf(fp,"(The HTML website structure is stored here to allow fast updates)"LF""LF);
  2183.           fprintf(fp,"DO NOT delete this folder unless you do not want to update the mirror in the future!!"LF);
  2184.           fprintf(fp,"(you can safely delete old.zip and old.lst files, however)"LF);
  2185.           fprintf(fp,""LF);
  2186.           fprintf(fp,HTS_LOG_SECURITY_WARNING);
  2187.           fclose(fp);
  2188.         }
  2189.       }
  2190.  
  2191.       sprintf(n_lock,fconcat(httrack.path_log,"hts-in_progress.lock"));
  2192.       //sprintf(n_lock,fconcat(httrack.path_log,"hts-in_progress.lock"),n);
  2193.       /*do {
  2194.         if (!n)
  2195.           sprintf(n_lock,fconcat(httrack.path_log,"hts-in_progress.lock"),n);
  2196.         else
  2197.           sprintf(n_lock,fconcat(httrack.path_log,"hts-in_progress%d.lock"),n);
  2198.         n++;
  2199.       } while((fexist(n_lock)) && httrack.quiet);      
  2200.       if (fexist(n_lock)) {
  2201.         if (!recuperer) {
  2202.           remove(n_lock);
  2203.         }
  2204.       }*/
  2205.  
  2206.       // vΘrifier existence de la structure
  2207.       structcheck(fconcat(httrack.path_html, "/"));
  2208.       structcheck(fconcat(httrack.path_log, "/"));
  2209.      
  2210.       // reprise/update
  2211.       if (httrack.cache) {
  2212.         FILE* fp;
  2213.         int i;
  2214. #if HTS_WIN
  2215.         mkdir(fconcat(httrack.path_log,"hts-cache"));
  2216. #else
  2217.         mkdir(fconcat(httrack.path_log,"hts-cache"),HTS_PROTECT_FOLDER);
  2218. #endif
  2219.         fp=fopen(fconcat(httrack.path_log,"hts-cache/doit.log"),"wb");
  2220.         if (fp) {
  2221.           for(i=0+1;i<argc;i++) {
  2222.             if ( ((strchr(argv[i],' ')!=NULL) || (strchr(argv[i],'"')!=NULL) || (strchr(argv[i],'\\')!=NULL)) && (argv[i][0]!='"')  ) {
  2223.               int j;
  2224.               fprintf(fp,"\"");
  2225.               for(j=0;j<(int) strlen(argv[i]);j++) {
  2226.                 if (argv[i][j]==34)
  2227.                   fprintf(fp,"\\\"");
  2228.                 else if (argv[i][j]=='\\')
  2229.                   fprintf(fp,"\\\\");
  2230.                 else
  2231.                   fprintf(fp,"%c",argv[i][j]);
  2232.               }
  2233.               fprintf(fp,"\"");
  2234.             } else if (strnotempty(argv[i])==0) {   // ""
  2235.               fprintf(fp,"\"\"");
  2236.             } else {   // non critique
  2237.               fprintf(fp,"%s",argv[i]);
  2238.             }
  2239.             if (i<argc-1)
  2240.               fprintf(fp," ");
  2241.           }
  2242.           fprintf(fp,LF);
  2243.           fprintf(fp,"File generated automatically on %s, do NOT edit"LF,t);
  2244.           fprintf(fp,LF);
  2245.           fprintf(fp,"To update a mirror, just launch httrack without any parameters"LF);
  2246.           fprintf(fp,"The existing cache will be used (and modified)"LF);
  2247.           fprintf(fp,"To have other options, retype all parameters and launch HTTrack"LF);
  2248.           fprintf(fp,"To continue an interrupted mirror, just launch httrack without any parameters"LF);
  2249.           fprintf(fp,LF);
  2250.           fclose(fp); fp=NULL;
  2251.         //} else if (httrack.debug>1) {
  2252.         //  printf("! FileOpen error, \"%s\"\n",strerror(errno));
  2253.         }
  2254.       }
  2255.       
  2256.       // petit message dans le lock
  2257.       if ( (fp=fopen(n_lock,"wb"))!=NULL) {
  2258.         int i;
  2259.         fprintf(fp,"Mirror in progress since %s .. please wait!"LF,t);
  2260.         for(i=0;i<argc;i++) {
  2261.           if (strchr(argv[i],' ')==NULL)
  2262.             fprintf(fp,"%s ",argv[i]);
  2263.           else    // entre ""
  2264.             fprintf(fp,"\"%s\" ",argv[i]);
  2265.         }
  2266.         fprintf(fp,LF);
  2267.         fprintf(fp, "To pause the engine: create an empty file named 'hts-stop.lock'"LF);
  2268.         fclose(fp); fp=NULL;
  2269.       }
  2270.       
  2271.       // fichier log        
  2272.       if (httrack.log)     {
  2273.         int i;
  2274.         fprintf(httrack.log,"HTTrack"HTTRACK_VERSION"%s launched on %s at %s"LF, 
  2275.           WHAT_is_available,
  2276.           t, url);
  2277.         fprintf(httrack.log,"(");
  2278.         for(i=0;i<argc;i++) {
  2279.           if ((strchr(argv[i],' ')==NULL) || (strchr(argv[i],'\"')))
  2280.             fprintf(httrack.log,"%s ",argv[i]);
  2281.           else    // entre "" (si espace(s) et pas dΘja de ")
  2282.             fprintf(httrack.log,"\"%s\" ",argv[i]);
  2283.         }
  2284.         fprintf(httrack.log,")"LF);
  2285.         fprintf(httrack.log,LF);
  2286.         fprintf(httrack.log,"Information, Warnings and Errors reported for this mirror:"LF);
  2287.         fprintf(httrack.log,HTS_LOG_SECURITY_WARNING );
  2288.         fprintf(httrack.log,LF);
  2289.       }
  2290.  
  2291.       if (httrack_logmode) {
  2292.         printf("Mirror launched on %s by HTTrack Website Copier/"HTTRACK_VERSION"%s "HTTRACK_AFF_AUTHORS""LF,t,WHAT_is_available);
  2293.         if (httrack.wizard==0) {
  2294.           printf("mirroring %s with %d levels, %d sockets,t=%d,s=%d,logm=%d,lnk=%d,mdg=%d\n",url,httrack.depth,httrack.maxsoc,httrack.travel,httrack.seeker,httrack_logmode,httrack.urlmode,httrack.getmode);
  2295.         } else {    // the magic wizard
  2296.           printf("mirroring %s with the wizard help..\n",url);
  2297.         }
  2298.       }
  2299.     }
  2300.     
  2301.     io_flush;
  2302.  
  2303.   /* Info for wrappers */
  2304.   if ( (httrack.debug>0) && (httrack.log!=NULL) ) {
  2305.     fspc(httrack.log,"info"); fprintf(httrack.log,"engine: init"LF);
  2306.   }
  2307. #if HTS_ANALYSTE
  2308.   hts_htmlcheck_init();
  2309. #endif
  2310.  
  2311.   // dΘtourner SIGHUP etc.
  2312. #if HTS_WIN
  2313.   signal( SIGINT  , sig_ask    );   // ^C
  2314.   signal( SIGTERM , sig_finish );   // kill <process>
  2315. #else
  2316.   signal( SIGHUP  , sig_back   );   // close window
  2317.   signal( SIGTSTP , sig_back   );   // ^Z
  2318.   signal( SIGTERM , sig_finish );   // kill <process>
  2319.   signal( SIGINT  , sig_ask    );   // ^C
  2320.   signal( SIGPIPE , sig_brpipe );   // broken pipe (write into non-opened socket)
  2321. /*
  2322. deprecated - see SIGCHLD
  2323. #ifndef HTS_DO_NOT_SIGCLD
  2324.   signal( SIGCLD  , sig_ignore );   // child change status
  2325. #endif
  2326. */
  2327.   signal( SIGCHLD , sig_ignore );   // child change status
  2328. #endif
  2329. #if DEBUG_STEPS
  2330.   printf("Launching the mirror\n");
  2331. #endif
  2332.   
  2333.  
  2334.     // Lancement du miroir
  2335.     // ------------------------------------------------------------
  2336.     if (httpmirror(url, &httrack)==0) {
  2337.       printf("Error during operation (see log file), site has not been successfully mirrored\n");
  2338.     } else {
  2339.       if  (httrack.shell) {
  2340.         HTT_REQUEST_START;
  2341.         HT_PRINT("TRANSFER DONE"LF);
  2342.         HTT_REQUEST_END
  2343.       } else {
  2344.         printf("Done.\n");
  2345.       }
  2346.     }
  2347.     // ------------------------------------------------------------
  2348.  
  2349.     //
  2350.     // Build top index
  2351.     if (httrack.dir_topindex) {
  2352.       char rpath[1024*2];
  2353.       char* a;
  2354.       strcpybuff(rpath,httrack.path_html);
  2355.       if (rpath[0]) {
  2356.         if (rpath[strlen(rpath)-1]=='/')
  2357.           rpath[strlen(rpath)-1]='\0';
  2358.       }
  2359.       a=strrchr(rpath,'/');
  2360.       if (a) {
  2361.         *a='\0';
  2362.         hts_buildtopindex(&httrack,rpath,httrack.path_bin);
  2363.         if (httrack.log) {
  2364.           fspc(httrack.log,"info"); fprintf(httrack.log,"Top index rebuilt (done)"LF);
  2365.         }
  2366.       }
  2367.     }
  2368.  
  2369.     if (exit_xh ==1) {
  2370.       if (httrack.log) {
  2371.         fprintf(httrack.log,"* * MIRROR ABORTED! * *\nThe current temporary cache is required for any update operation and only contains data downloaded during the present aborted session.\nThe former cache might contain more complete information; if you do not want to lose that information, you have to restore it and delete the current cache.\nThis can easily be done here by erasing the hts-cache/new.* files]\n");
  2372.       }
  2373.     }
  2374.  
  2375.   /* Info for wrappers */
  2376.   if ( (httrack.debug>0) && (httrack.log!=NULL) ) {
  2377.     fspc(httrack.log,"info"); fprintf(httrack.log,"engine: free"LF);
  2378.   }
  2379. #if HTS_ANALYSTE
  2380.   hts_htmlcheck_uninit();
  2381. #endif
  2382.     
  2383.     if (httrack_logmode!=1) {
  2384.       if (httrack.errlog == httrack.log) httrack.errlog=NULL;
  2385.       if (httrack.log) { fclose(httrack.log); httrack.log=NULL; }
  2386.       if (httrack.errlog) { fclose(httrack.errlog); httrack.errlog=NULL; }
  2387.     }  
  2388.     
  2389.     // DΘbuggage des en tΩtes
  2390.     if (_DEBUG_HEAD) {
  2391.       if (ioinfo) {
  2392.         fclose(ioinfo);
  2393.       }
  2394.     }
  2395.     
  2396.     // supprimer lock
  2397.     remove(n_lock);
  2398.   }
  2399.   
  2400.   if (x_argvblk)
  2401.     freet(x_argvblk);
  2402.   if (x_argv)
  2403.     freet(x_argv);
  2404.  
  2405. #if HTS_WIN
  2406. #if HTS_ANALYSTE!=2
  2407. //  WSACleanup();    // ** non en cas de thread tjs prΘsent!..
  2408. #endif
  2409. #endif
  2410. #ifdef HTS_TRACE_MALLOC
  2411.   hts_freeall();
  2412. #endif
  2413.  
  2414.   printf("Thanks for using HTTrack!\n");
  2415.   io_flush;
  2416.   htsmain_free();
  2417.   return 0;    // OK
  2418. }
  2419.  
  2420.  
  2421. // main() subroutines
  2422.  
  2423. // vΘrifier chemin path
  2424. int check_path(char* s,char* defaultname) {
  2425.   int i;
  2426.   int return_value=0;
  2427.  
  2428.   // Replace name: ~/mywebsites/# -> /home/foo/mywebsites/#
  2429.   expand_home(s);
  2430.   for(i=0;i<(int) strlen(s);i++)    // conversion \ -> /
  2431.     if (s[i]=='\\')
  2432.       s[i]='/';
  2433.   
  2434.   // remove ending /
  2435.   if (strnotempty(s))
  2436.   if (s[strlen(s)-1]=='/')
  2437.     s[strlen(s)-1]='\0';
  2438.  
  2439.    // Replace name: /home/foo/mywebsites/# -> /home/foo/mywebsites/wonderfulsite
  2440.   if (strnotempty(s)) {
  2441.     if (s[(i=strlen(s))-1]=='#') {
  2442.       if (strnotempty((defaultname?defaultname:""))) {
  2443.         char tempo[HTS_URLMAXSIZE*2];
  2444.         char* a=strchr(defaultname,'#');      // we never know..
  2445.         if (a) *a='\0';
  2446.         tempo[0]='\0';
  2447.         strncatbuff(tempo,s,i-1);
  2448.         strcatbuff(tempo,defaultname);
  2449.         strcpybuff(s,tempo);
  2450.       } else
  2451.         s[0]='\0';            // Clear path (no name/default url given)
  2452.       return_value=1;     // expanded
  2453.     }
  2454.   }
  2455.  
  2456.   // ending /
  2457.   if (strnotempty(s))
  2458.   if (s[strlen(s)-1]!='/')    // ajouter slash α la fin
  2459.     strcatbuff(s,"/");
  2460.  
  2461.   return return_value;
  2462. }
  2463.  
  2464. // dΘtermine si l'argument est une option
  2465. int cmdl_opt(char* s) {
  2466.   if (s[0]=='-') {  // c'est peut Ωtre une option
  2467.     if (strchr(s,'.')!=NULL)
  2468.       return 0;    // sans doute un -www.truc.fr (note: -www n'est pas compris)
  2469.     else if (strchr(s,'/')!=NULL)
  2470.       return 0;    // idem, -*cgi-bin/
  2471.     else if (strchr(s,'*')!=NULL)
  2472.       return 0;    // joker, idem
  2473.     else
  2474.       return 1;
  2475.   } else return 0;
  2476. }
  2477.  
  2478.